Migrate NLS access to preferencecontrollers
In preparation for some new settings Test: RoboTests Bug: 173052211 Change-Id: I57c692d7ff0a1a8e36fb9e3f6c159263997fdc71
This commit is contained in:
@@ -8587,7 +8587,7 @@
|
|||||||
<string name="recent_conversations">Recent conversations</string>
|
<string name="recent_conversations">Recent conversations</string>
|
||||||
|
|
||||||
<!-- [CHAR LIMIT=20] button title -->
|
<!-- [CHAR LIMIT=20] button title -->
|
||||||
<string name="conversation_settings_clear_recents">Clear recents</string>
|
<string name="conversation_settings_clear_recents">Clear all of the recent ones</string>
|
||||||
|
|
||||||
<!-- a11y string -->
|
<!-- a11y string -->
|
||||||
<string name="clear">Clear</string>
|
<string name="clear">Clear</string>
|
||||||
|
@@ -17,11 +17,18 @@
|
|||||||
|
|
||||||
<PreferenceScreen
|
<PreferenceScreen
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:settings="http://schemas.android.com/apk/res-auto"
|
||||||
android:key="notification_access_permission_detail_settings"
|
android:key="notification_access_permission_detail_settings"
|
||||||
android:title="@string/manage_notification_access_title">
|
android:title="@string/manage_notification_access_title">
|
||||||
|
|
||||||
|
<com.android.settingslib.widget.LayoutPreference
|
||||||
|
android:key="pref_app_header"
|
||||||
|
android:layout="@layout/settings_entity_header"
|
||||||
|
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.HeaderPreferenceController"/>
|
||||||
|
|
||||||
<com.android.settings.widget.FilterTouchesSwitchPreference
|
<com.android.settings.widget.FilterTouchesSwitchPreference
|
||||||
android:key="notification_access_switch"
|
android:key="notification_access_switch"
|
||||||
android:title="@string/notification_access_detail_switch"/>
|
android:title="@string/notification_access_detail_switch"
|
||||||
|
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.ApprovalPreferenceController"/>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 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.applications.specialaccess.notificationaccess;
|
||||||
|
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.settings.SettingsEnums;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
|
import androidx.preference.SwitchPreference;
|
||||||
|
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
|
||||||
|
public class ApprovalPreferenceController extends BasePreferenceController {
|
||||||
|
|
||||||
|
private static final String TAG = "ApprovalPrefController";
|
||||||
|
|
||||||
|
private PackageInfo mPkgInfo;
|
||||||
|
private ComponentName mCn;
|
||||||
|
private PreferenceFragmentCompat mParent;
|
||||||
|
private NotificationManager mNm;
|
||||||
|
private PackageManager mPm;
|
||||||
|
|
||||||
|
public ApprovalPreferenceController(Context context, String key) {
|
||||||
|
super(context, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApprovalPreferenceController setPkgInfo(PackageInfo pkgInfo) {
|
||||||
|
mPkgInfo = pkgInfo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApprovalPreferenceController setCn(ComponentName cn) {
|
||||||
|
mCn = cn;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApprovalPreferenceController setParent(PreferenceFragmentCompat parent) {
|
||||||
|
mParent = parent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApprovalPreferenceController setNm(NotificationManager nm) {
|
||||||
|
mNm = nm;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApprovalPreferenceController setPm(PackageManager pm) {
|
||||||
|
mPm = pm;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference pref) {
|
||||||
|
final SwitchPreference preference = (SwitchPreference) pref;
|
||||||
|
final CharSequence label = mPkgInfo.applicationInfo.loadLabel(mPm);
|
||||||
|
preference.setChecked(isServiceEnabled(mCn));
|
||||||
|
preference.setOnPreferenceChangeListener((p, newValue) -> {
|
||||||
|
final boolean access = (Boolean) newValue;
|
||||||
|
if (!access) {
|
||||||
|
if (!isServiceEnabled(mCn)) {
|
||||||
|
return true; // already disabled
|
||||||
|
}
|
||||||
|
// show a friendly dialog
|
||||||
|
new FriendlyWarningDialogFragment()
|
||||||
|
.setServiceInfo(mCn, label, mParent)
|
||||||
|
.show(mParent.getFragmentManager(), "friendlydialog");
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
if (isServiceEnabled(mCn)) {
|
||||||
|
return true; // already enabled
|
||||||
|
}
|
||||||
|
// show a scary dialog
|
||||||
|
new ScaryWarningDialogFragment()
|
||||||
|
.setServiceInfo(mCn, label, mParent)
|
||||||
|
.show(mParent.getFragmentManager(), "dialog");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disable(final ComponentName cn) {
|
||||||
|
logSpecialPermissionChange(true, cn.getPackageName());
|
||||||
|
mNm.setNotificationListenerAccessGranted(cn, false);
|
||||||
|
AsyncTask.execute(() -> {
|
||||||
|
if (!mNm.isNotificationPolicyAccessGrantedForPackage(
|
||||||
|
cn.getPackageName())) {
|
||||||
|
mNm.removeAutomaticZenRules(cn.getPackageName());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void enable(ComponentName cn) {
|
||||||
|
logSpecialPermissionChange(true, cn.getPackageName());
|
||||||
|
mNm.setNotificationListenerAccessGranted(cn, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isServiceEnabled(ComponentName cn) {
|
||||||
|
return mNm.isNotificationListenerAccessGranted(cn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void logSpecialPermissionChange(boolean enable, String packageName) {
|
||||||
|
final int logCategory = enable ? SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW
|
||||||
|
: SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_DENY;
|
||||||
|
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider().action(mContext,
|
||||||
|
logCategory, packageName);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 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.applications.specialaccess.notificationaccess;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.util.IconDrawableFactory;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import androidx.lifecycle.LifecycleObserver;
|
||||||
|
import androidx.lifecycle.OnLifecycleEvent;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
|
import com.android.settings.widget.EntityHeaderController;
|
||||||
|
import com.android.settingslib.applications.AppUtils;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
import com.android.settingslib.widget.LayoutPreference;
|
||||||
|
|
||||||
|
public class HeaderPreferenceController extends BasePreferenceController
|
||||||
|
implements PreferenceControllerMixin, LifecycleObserver {
|
||||||
|
|
||||||
|
private DashboardFragment mFragment;
|
||||||
|
private EntityHeaderController mHeaderController;
|
||||||
|
private PackageInfo mPackageInfo;
|
||||||
|
private PackageManager mPm;
|
||||||
|
private CharSequence mServiceName;
|
||||||
|
|
||||||
|
public HeaderPreferenceController(Context context, String key) {
|
||||||
|
super(context, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderPreferenceController setFragment(DashboardFragment fragment) {
|
||||||
|
mFragment = fragment;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderPreferenceController setPackageInfo(PackageInfo packageInfo) {
|
||||||
|
mPackageInfo = packageInfo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderPreferenceController setPm(PackageManager pm) {
|
||||||
|
mPm = pm;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderPreferenceController setServiceName(CharSequence serviceName) {
|
||||||
|
mServiceName = serviceName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
if (mFragment == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LayoutPreference pref = screen.findPreference(getPreferenceKey());
|
||||||
|
mHeaderController = EntityHeaderController.newInstance(
|
||||||
|
mFragment.getActivity(), mFragment, pref.findViewById(R.id.entity_header));
|
||||||
|
pref = mHeaderController
|
||||||
|
.setRecyclerView(mFragment.getListView(), mFragment.getSettingsLifecycle())
|
||||||
|
.setIcon(IconDrawableFactory.newInstance(mFragment.getActivity())
|
||||||
|
.getBadgedIcon(mPackageInfo.applicationInfo))
|
||||||
|
.setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
|
||||||
|
.setSummary(mServiceName)
|
||||||
|
.setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
|
||||||
|
.setPackageName(mPackageInfo.packageName)
|
||||||
|
.setUid(mPackageInfo.applicationInfo.uid)
|
||||||
|
.setHasAppInfoLink(true)
|
||||||
|
.setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
|
||||||
|
EntityHeaderController.ActionType.ACTION_NONE)
|
||||||
|
.done(mFragment.getActivity(), mContext);
|
||||||
|
pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_START)
|
||||||
|
public void onStart() {
|
||||||
|
if (mHeaderController != null) {
|
||||||
|
mHeaderController.styleActionBar(mFragment.getActivity());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -16,52 +16,56 @@
|
|||||||
|
|
||||||
package com.android.settings.applications.specialaccess.notificationaccess;
|
package com.android.settings.applications.specialaccess.notificationaccess;
|
||||||
|
|
||||||
|
import static com.android.settings.applications.AppInfoBase.ARG_PACKAGE_NAME;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.pm.ServiceInfo;
|
import android.content.pm.ServiceInfo;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.service.notification.NotificationListenerService;
|
import android.service.notification.NotificationListenerService;
|
||||||
import android.util.IconDrawableFactory;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Slog;
|
import android.util.Slog;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.SwitchPreference;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.applications.AppInfoBase;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.applications.manageapplications.ManageApplications;
|
||||||
import com.android.settings.widget.EntityHeaderController;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settingslib.applications.AppUtils;
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
|
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||||
|
import com.android.settingslib.applications.ApplicationsState;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class NotificationAccessDetails extends AppInfoBase {
|
public class NotificationAccessDetails extends DashboardFragment {
|
||||||
private static final String TAG = "NotifAccessDetails";
|
private static final String TAG = "NotifAccessDetails";
|
||||||
private static final String SWITCH_PREF_KEY = "notification_access_switch";
|
|
||||||
|
|
||||||
private boolean mCreated;
|
|
||||||
private ComponentName mComponentName;
|
private ComponentName mComponentName;
|
||||||
private CharSequence mServiceName;
|
private CharSequence mServiceName;
|
||||||
|
protected PackageInfo mPackageInfo;
|
||||||
|
protected int mUserId;
|
||||||
|
protected String mPackageName;
|
||||||
|
protected RestrictedLockUtils.EnforcedAdmin mAppsControlDisallowedAdmin;
|
||||||
|
protected boolean mAppsControlDisallowedBySystem;
|
||||||
private boolean mIsNls;
|
private boolean mIsNls;
|
||||||
|
|
||||||
private NotificationManager mNm;
|
|
||||||
private PackageManager mPm;
|
private PackageManager mPm;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
if (mComponentName == null && intent != null) {
|
if (mComponentName == null && intent != null) {
|
||||||
String cn = intent.getStringExtra(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME);
|
String cn = intent.getStringExtra(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME);
|
||||||
@@ -73,38 +77,20 @@ public class NotificationAccessDetails extends AppInfoBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
mNm = getContext().getSystemService(NotificationManager.class);
|
|
||||||
mPm = getPackageManager();
|
mPm = getPackageManager();
|
||||||
addPreferencesFromResource(R.xml.notification_access_permission_details);
|
retrieveAppEntry();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
|
||||||
super.onActivityCreated(savedInstanceState);
|
|
||||||
if (mCreated) {
|
|
||||||
Log.w(TAG, "onActivityCreated: ignoring duplicate call");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
mCreated = true;
|
|
||||||
if (mPackageInfo == null) return;
|
|
||||||
loadNotificationListenerService();
|
loadNotificationListenerService();
|
||||||
final Activity activity = getActivity();
|
use(ApprovalPreferenceController.class)
|
||||||
final Preference pref = EntityHeaderController
|
.setPkgInfo(mPackageInfo)
|
||||||
.newInstance(activity, this, null /* header */)
|
.setCn(mComponentName)
|
||||||
.setRecyclerView(getListView(), getSettingsLifecycle())
|
.setNm(context.getSystemService(NotificationManager.class))
|
||||||
.setIcon(IconDrawableFactory.newInstance(getContext())
|
.setPm(context.getPackageManager())
|
||||||
.getBadgedIcon(mPackageInfo.applicationInfo))
|
.setParent(this);
|
||||||
.setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
|
use(HeaderPreferenceController.class)
|
||||||
.setSummary(mServiceName)
|
.setFragment(this)
|
||||||
.setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
|
.setPackageInfo(mPackageInfo)
|
||||||
.setPackageName(mPackageName)
|
.setPm(context.getPackageManager())
|
||||||
.setUid(mPackageInfo.applicationInfo.uid)
|
.setServiceName(mServiceName);
|
||||||
.setHasAppInfoLink(true)
|
|
||||||
.setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
|
|
||||||
EntityHeaderController.ActionType.ACTION_NONE)
|
|
||||||
.done(activity, getPrefContext());
|
|
||||||
getPreferenceScreen().addPreference(pref);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -112,9 +98,7 @@ public class NotificationAccessDetails extends AppInfoBase {
|
|||||||
return SettingsEnums.NOTIFICATION_ACCESS_DETAIL;
|
return SettingsEnums.NOTIFICATION_ACCESS_DETAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean refreshUi() {
|
protected boolean refreshUi() {
|
||||||
final Context context = getContext();
|
|
||||||
if (mComponentName == null) {
|
if (mComponentName == null) {
|
||||||
// No service given
|
// No service given
|
||||||
Slog.d(TAG, "No component name provided");
|
Slog.d(TAG, "No component name provided");
|
||||||
@@ -130,72 +114,74 @@ public class NotificationAccessDetails extends AppInfoBase {
|
|||||||
Slog.d(TAG, "NLSes aren't allowed in work profiles");
|
Slog.d(TAG, "NLSes aren't allowed in work profiles");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
updatePreference(findPreference(SWITCH_PREF_KEY));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AlertDialog createDialog(int id, int errorCode) {
|
public void onResume() {
|
||||||
return null;
|
super.onResume();
|
||||||
|
mAppsControlDisallowedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
|
||||||
|
getActivity(), UserManager.DISALLOW_APPS_CONTROL, mUserId);
|
||||||
|
mAppsControlDisallowedBySystem = RestrictedLockUtilsInternal.hasBaseUserRestriction(
|
||||||
|
getActivity(), UserManager.DISALLOW_APPS_CONTROL, mUserId);
|
||||||
|
|
||||||
|
if (!refreshUi()) {
|
||||||
|
setIntentAndFinish(true /* appChanged */);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePreference(SwitchPreference preference) {
|
protected void setIntentAndFinish(boolean appChanged) {
|
||||||
final CharSequence label = mPackageInfo.applicationInfo.loadLabel(mPm);
|
Log.i(TAG, "appChanged=" + appChanged);
|
||||||
preference.setChecked(isServiceEnabled(mComponentName));
|
Intent intent = new Intent();
|
||||||
preference.setOnPreferenceChangeListener((p, newValue) -> {
|
intent.putExtra(ManageApplications.APP_CHG, appChanged);
|
||||||
final boolean access = (Boolean) newValue;
|
SettingsActivity sa = (SettingsActivity) getActivity();
|
||||||
if (!access) {
|
sa.finishPreferencePanel(Activity.RESULT_OK, intent);
|
||||||
if (!isServiceEnabled(mComponentName)) {
|
}
|
||||||
return true; // already disabled
|
|
||||||
}
|
protected void retrieveAppEntry() {
|
||||||
// show a friendly dialog
|
final Bundle args = getArguments();
|
||||||
new FriendlyWarningDialogFragment()
|
mPackageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null;
|
||||||
.setServiceInfo(mComponentName, label, this)
|
Intent intent = (args == null) ?
|
||||||
.show(getFragmentManager(), "friendlydialog");
|
getIntent() : (Intent) args.getParcelable("intent");
|
||||||
return false;
|
if (mPackageName == null) {
|
||||||
} else {
|
if (intent != null && intent.getData() != null) {
|
||||||
if (isServiceEnabled(mComponentName)) {
|
mPackageName = intent.getData().getSchemeSpecificPart();
|
||||||
return true; // already enabled
|
|
||||||
}
|
|
||||||
// show a scary dialog
|
|
||||||
new ScaryWarningDialogFragment()
|
|
||||||
.setServiceInfo(mComponentName, label, this)
|
|
||||||
.show(getFragmentManager(), "dialog");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
if (intent != null && intent.hasExtra(Intent.EXTRA_USER_HANDLE)) {
|
||||||
|
mUserId = ((UserHandle) intent.getParcelableExtra(
|
||||||
@VisibleForTesting
|
Intent.EXTRA_USER_HANDLE)).getIdentifier();
|
||||||
void logSpecialPermissionChange(boolean enable, String packageName) {
|
} else {
|
||||||
int logCategory = enable ? SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW
|
mUserId = UserHandle.myUserId();
|
||||||
: SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_DENY;
|
}
|
||||||
FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action(getContext(),
|
|
||||||
logCategory, packageName);
|
try {
|
||||||
|
mPackageInfo = mPm.getPackageInfoAsUser(mPackageName,
|
||||||
|
PackageManager.MATCH_DISABLED_COMPONENTS |
|
||||||
|
PackageManager.GET_SIGNING_CERTIFICATES |
|
||||||
|
PackageManager.GET_PERMISSIONS, mUserId);
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
Log.e(TAG, "Exception when retrieving package:" + mPackageName, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dialogs only have access to the parent fragment, not the controller, so pass the information
|
||||||
|
// along to keep business logic out of this file
|
||||||
public void disable(final ComponentName cn) {
|
public void disable(final ComponentName cn) {
|
||||||
logSpecialPermissionChange(true, cn.getPackageName());
|
final PreferenceScreen screen = getPreferenceScreen();
|
||||||
mNm.setNotificationListenerAccessGranted(cn, false);
|
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
|
||||||
AsyncTask.execute(() -> {
|
controller.disable(cn);
|
||||||
if (!mNm.isNotificationPolicyAccessGrantedForPackage(
|
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
|
||||||
cn.getPackageName())) {
|
|
||||||
mNm.removeAutomaticZenRules(cn.getPackageName());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
refreshUi();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void enable(ComponentName cn) {
|
protected void enable(ComponentName cn) {
|
||||||
logSpecialPermissionChange(true, cn.getPackageName());
|
final PreferenceScreen screen = getPreferenceScreen();
|
||||||
mNm.setNotificationListenerAccessGranted(cn, true);
|
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
|
||||||
refreshUi();
|
controller.enable(cn);
|
||||||
}
|
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
|
||||||
|
|
||||||
protected boolean isServiceEnabled(ComponentName cn) {
|
|
||||||
return mNm.isNotificationListenerAccessGranted(cn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// To save binder calls, load this in the fragment rather than each preference controller
|
||||||
protected void loadNotificationListenerService() {
|
protected void loadNotificationListenerService() {
|
||||||
mIsNls = false;
|
mIsNls = false;
|
||||||
|
|
||||||
@@ -218,4 +204,14 @@ public class NotificationAccessDetails extends AppInfoBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getPreferenceScreenResId() {
|
||||||
|
return R.xml.notification_access_permission_details;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2019 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.applications.specialaccess.notificationaccess;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
|
||||||
import static org.mockito.Mockito.spy;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class NotificationAccessDetailsTest {
|
|
||||||
|
|
||||||
private Context mContext;
|
|
||||||
private FakeFeatureFactory mFeatureFactory;
|
|
||||||
private NotificationAccessDetails mFragment;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
|
||||||
mFragment = spy(new NotificationAccessDetails());
|
|
||||||
mContext = RuntimeEnvironment.application;
|
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void logSpecialPermissionChange() {
|
|
||||||
mFragment.logSpecialPermissionChange(true, "app");
|
|
||||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
|
||||||
mContext,
|
|
||||||
MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW,
|
|
||||||
"app");
|
|
||||||
|
|
||||||
mFragment.logSpecialPermissionChange(false, "app");
|
|
||||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
|
||||||
mContext,
|
|
||||||
MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_DENY,
|
|
||||||
"app");
|
|
||||||
}
|
|
||||||
}
|
|
@@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 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.applications.specialaccess.notificationaccess;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
|
import androidx.preference.SwitchPreference;
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class ApprovalPreferenceControllerTest {
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
|
@Mock
|
||||||
|
private NotificationAccessDetails mFragment;
|
||||||
|
private ApprovalPreferenceController mController;
|
||||||
|
@Mock
|
||||||
|
NotificationManager mNm;
|
||||||
|
@Mock
|
||||||
|
PackageManager mPm;
|
||||||
|
PackageInfo mPkgInfo;
|
||||||
|
ComponentName mCn = new ComponentName("a", "b");
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
|
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
|
|
||||||
|
mPkgInfo = new PackageInfo();
|
||||||
|
mPkgInfo.applicationInfo = mock(ApplicationInfo.class);
|
||||||
|
when(mPkgInfo.applicationInfo.loadLabel(mPm)).thenReturn("LABEL");
|
||||||
|
|
||||||
|
mController = new ApprovalPreferenceController(mContext, "key");
|
||||||
|
mController.setCn(mCn);
|
||||||
|
mController.setNm(mNm);
|
||||||
|
mController.setParent(mFragment);
|
||||||
|
mController.setPkgInfo(mPkgInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateState_checked() {
|
||||||
|
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
|
||||||
|
SwitchPreference pref = new SwitchPreference(mContext);
|
||||||
|
|
||||||
|
mController.updateState(pref);
|
||||||
|
assertThat(pref.isChecked()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enable() {
|
||||||
|
mController.enable(mCn);
|
||||||
|
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||||
|
mContext,
|
||||||
|
MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW,
|
||||||
|
"a");
|
||||||
|
|
||||||
|
verify(mNm).setNotificationListenerAccessGranted(mCn, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void disable() {
|
||||||
|
mController.disable(mCn);
|
||||||
|
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||||
|
mContext,
|
||||||
|
MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW,
|
||||||
|
"a");
|
||||||
|
|
||||||
|
verify(mNm).setNotificationListenerAccessGranted(mCn, false);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user