Reputation: 4451
Google have recently released Android Marshmallow with Now on Tap which can scan the app contents and provide an additional information to the user.
Unfortunately, for our app the info doesn't look very relevant and Google is ignoring the data we set inside onProvideContentAssist()
and onProvideAssistData()
.
These specs look rather high level and also contain the words like "can suggest" and "additional information" so seems like Google officially allows itself to ignore the data app developers provide.
So we decided to disable Now on Tap, but seems it is not very trivial.
According to the doc provided above, we should use FLAG_SECURE
in this case. But then users couldn't capture screenshots and Google Now on Tap starts to blame our app with the following user facing message:
Results not available
This app has blocked Now on Tap
But it seems that Opera somehow gently blocks Now on Tap for their private tabs. It shows much more app friendly message for them:
Nothing on tap
How does Opera block Now on Tap?
Does anybody know how to block Google Assist API (Now on Tap) without getting a blame regarding our app from their side?
Upvotes: 9
Views: 3388
Reputation: 1006644
Another possible solution, that I'll eventually need to try out, is hinted at by hackbod's one comment:
Further, in this case there is no reason to do this, because you can use this: https://developer.android.com/reference/android/view/View.html#dispatchProvideStructure(android.view.ViewStructure)
The code comment for dispatchProvideStructure()
states: "Dispatch creation of ViewStructure
down the hierarchy." (emphasis mine).
So, create a NoAssistFrameLayout
subclass of FrameLayout
, override dispatchProvideStructure()
to be a no-op, and use that as your root container for your activity layout. This will block the automatic population of the ViewStructure
for anything in the body of the activity.
It will not, however, block anything that could be inferred from stuff outside your main content area, such as your action bar, as that would be outside the NoAssistFrameLayout
's view hierarchy. Probably there's not much in there that would have privacy implications, but it's a limitation of this technique.
OK, I have tried it, and it does seem to work:
/***
Copyright (c) 2015 CommonsWare, LLC
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.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.assist.no;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewStructure;
import android.widget.FrameLayout;
public class NoAssistFrameLayout extends FrameLayout {
public NoAssistFrameLayout(Context context) {
super(context);
}
public NoAssistFrameLayout(Context context,
AttributeSet attrs) {
super(context, attrs);
}
public NoAssistFrameLayout(Context context,
AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoAssistFrameLayout(Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void dispatchProvideStructure(ViewStructure structure) {
// no, thanks
}
}
Upvotes: 5
Reputation: 91331
Opera does this probably because they haven't updated their browser to report assist data (this requires special cooperation with apps that don't use the normal view hierarchy, like browsers).
However if your issue is that Now on Tap isn't returning relevant data, the solution is not to block the data it is getting -- all that does is ensure the data it returns for your app never improves, because it never sees data it could give better suggestions for. Blocking it certainly isn't going to cause it to use other data, it is just going to make it stupid for your app.
You should not be trying to make Now on Tap stupid for your app.
Upvotes: -2
Reputation: 32780
The View class has a method setAssistBlocked:
Controls whether assist data collection from this view and its children is enabled (that is, whether {@link #onProvideStructure} and {@link #onProvideVirtualStructure} will be called). The default value is false, allowing normal assist collection. Setting this to false will disable assist collection.
@param enabled Set to true to disable assist data collection, or false (the default) to allow it.
Unfortunately this method is annotated with @hide
, so we can't see it or directly use it.
But we can use it with a small reflection call:
private void setAssistBlocked(View view, boolean blocked) {
try {
Method setAssistBlockedMethod = View.class.getMethod("setAssistBlocked", boolean.class);
setAssistBlockedMethod.invoke(view, blocked);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
To block all contents in the activity you can use:
final View rootContent = findViewById(android.R.id.content);
setAssistBlocked(rootContent, true);
Upvotes: 7