Read RemoteViews information with some black magic way.
See RemoteViews.
As of Android Pie, the system will notify you that you are using hidden APIs (in other words, non-SDK APIs) via reflection. This library aims to read remote view properties by reflection so you will see bunch of warnings if you use this library. And the severity of the warning may change in the future.
You can read RemoteViews
information like this.
public class NotificationWatcher extends NotificationListenerService {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
RemoteViews remoteViews = sbn.getNotification().contentView;
RemoteViewsInfo info = RemoteViewsReader.read(this, remoteViews);
// you can read informations from `info` object.
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
}
RemoteViewsInfo
contains following informations.
- ApplicationInfo: An information about an application which have built this
RemoteViews
. - Actions: A list of actions the application have committed on
RemoteViews
. - Layout Id: An identity of the
RemoteViews
layout.
The operations for RemoteViews
are stored as a list of Action
.
So you can find the action you want like this.
RemoteViewsInfo info = RemoteViewsReader.read(this, remoteViews);
for (RemoteViewsAction action : info.getActions()) {
}
RemoteViewsAction is an abstract type for each concrete operations. We have several types by default, according to AOSP implementation.
- BitmapRefrectionAction
Action forRemoteViews#setImageBitmap()
. - RefrectionAction
Action for various methods declared onRemoteViews
. - RefrectionWithoutParamsAction
Action for various methods without any parameters declared onRemoteViews
. - SetDrawableParamsAction
Action forRemoteViews#setDrawableParameters()
. - SetEmptyViewAction
Action forRemoteViews#setEmptyView()
. - SetOnClickFillInIntentAction
Action forRemoteViews#setOnClickFillInIntent()
. - SetOnClickPendingIntentAction
Action forRemoteViews#setOnClickPendingIntent()
. - SetPendingIntentTemplateAction
Action forRemoteViews#setPendingIntentTemplate()
. - SetRemoteViewsAdapterIntentAction
Action forRemoteViews#setRemoteAdapter()
. - SetRemoteViewsAdapterListAction
Action forRemoteViews#setRemoteAdapter()
. - TextViewDrawableAction
Action forRemoteViews#setTextViewCompoundDrawables()
andRemoteViews#setTextViewCompoundDrawablesRelative()
, . - TextViewDrawableColorFilterAction
Action forRemoteViews#setTextViewCompoundDrawablesRelativeColorFilter()
. - TextViewSizeAction
Action forRemoteViews#setTextViewTextSize()
. - ViewGroupAction
Action forRemoteViews#addView()
andRemoteViews#remoteView()
. - ViewPaddingAction
Action forRemoteViews#setViewPadding()
.
If you would like to use the specific type of action, you can deal with it like this.
RemoteViewsInfo info = RemoteViewsReader.read(this, remoteViews);
for (RemoteViewsAction action : info.getActions()) {
if (!(action instanceof BitmapRefrectionAction))
continue;
BitmapRefrectionAction concrete = (BitmapRefrectionAction)action;
Bitmap bmp = concrete.getBitmap();
}
Currently this library contains default actions in AOSP. I've found several types of additional customized actions on some devices, and they are not supported now.
Via Gradle
implementation 'com.github.keithyokoma:RemoteViewsReader:1.3.0@aar'
Apache License v2
Copyright (C) 2015 KeithYokoma, Inc. All rights reserved.
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.