From 57544123cd3a669f6e36ee54f643e3dc4d18fba7 Mon Sep 17 00:00:00 2001 From: Eugene Susla Date: Fri, 14 Apr 2017 10:42:59 -0700 Subject: [PATCH] API for notification listener for Companioon apps Test: 1. Trigger the confitrmation dialog. Ensure it looks exactly like the one from settings. 2. Call an API without associating the appa first Ensure exception is thrown with a message mentioning the need to associate 1st Change-Id: I0b280e9bcef7f9d21ce7c9dfeaf2703945481efd --- AndroidManifest.xml | 4 + ...otificationAccessConfirmationActivity.java | 137 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 src/com/android/settings/notification/NotificationAccessConfirmationActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b38cea74f7b..02351e5238f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2645,6 +2645,10 @@ android:value="true" /> + + + onAllow(); + p.mNegativeButtonText = getString(R.string.deny); + p.mNegativeButtonListener = (a, b) -> cancel(); + AlertController + .create(this, this, getWindow()) + .installContent(p); + } + + private void onAllow() { + String requiredPermission = Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; + try { + ServiceInfo serviceInfo = getPackageManager().getServiceInfo(mComponentName, 0); + if (!requiredPermission.equals(serviceInfo.permission)) { + Slog.e(LOG_TAG, + "Service " + mComponentName + " lacks permission " + requiredPermission); + return; + } + } catch (PackageManager.NameNotFoundException e) { + Slog.e(LOG_TAG, "Failed to get service info for " + mComponentName, e); + return; + } + + final SettingsStringUtil.SettingStringHelper setting = + new SettingsStringUtil.SettingStringHelper( + getContentResolver(), + Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, + mUserId); + setting.write(SettingsStringUtil.ComponentNameSet.add(setting.read(), mComponentName)); + + finish(); + } + + @Override + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { + return AlertActivity.dispatchPopulateAccessibilityEvent(this, event); + } + + @Override + public void cancel() { + finish(); + } + + @Override + public void dismiss() { + // This is called after the click, since we finish when handling the + // click, don't do that again here. + if (!isFinishing()) { + finish(); + } + } + + @Override + protected void onResume() { + super.onResume(); + mTouchOverlayManager.setOverlayAllowed(false); + } + + @Override + protected void onPause() { + super.onPause(); + mTouchOverlayManager.setOverlayAllowed(true); + } +}