Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable the intent filter from android manifest programmatically

In my activity I have a web view and in manifest.xml I have declared intent filter like this

 <activity
        android:name=".ui.socialNetwork.MySocialNetworkActivity"
        android:configChanges="orientation|screenSize"
        android:process=":fb"
        android:screenOrientation="portrait" >

    </activity>

    <activity-alias
        android:targetActivity=".ui.socialNetwork.MySocialNetworkActivity"
        android:name=".AliasMySocialNetworkActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.PROCESS_TEXT" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity-alias>

This is not launcher activity. The intent filter used here is for copy paste toolbar on web view long press. This works fine. In addition to this I want to use Webview.setOnLongClickListener() for additional options, and I implemented like this.

webView = (WebView) findViewById(R.id.webview);

PackageManager pm = getApplicationContext().getPackageManager();
    ComponentName compName =
            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
    pm.setComponentEnabledSetting(
            compName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);

    webView.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            WebView.HitTestResult hitResult = null;
            hitResult = webView.getHitTestResult();
            if (hitResult != null && hitResult.getExtra() != null) {
                final String hitRes = hitResult.getExtra();
                if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE || hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    Intent ImageSaveIntent = new Intent(getApplicationContext(), SaveImage.class);
                    ImageSaveIntent.putExtra("putImage", hitRes);
                    startActivity(ImageSaveIntent);
                }
                if (hitResult.getType() != WebView.HitTestResult.IMAGE_TYPE || hitResult.getType() != WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    PackageManager pm = getApplicationContext().getPackageManager();
                    ComponentName compName =
                            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
                    pm.setComponentEnabledSetting(
                            compName,
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);
                                       }
            }
            return true;
        }
    });

My problem is that

  1. If I use intent filter alone without webview.setOnLongClickListener(), I can copy paste the text in webview

  2. If I use webview.setOnLongClickListener() alone, I can do my additional options and it is working fine.

  3. If I implement both intent filter and webview.setOnLongClickListener(), I cannot copy paste the text from webview. webview.setOnLongClickListener() will work fine. Here I understood that both functionalities depends on longPress, But I want both to work together.

I searched Webview.HitResult options for TextType, but it is not having such option. https://developer.android.com/reference/android/webkit/WebView.HitTestResult.html

like image 998
Praveen Kumar Avatar asked Nov 09 '16 11:11

Praveen Kumar


People also ask

Is it possible to remove and add intent filters from activity based on user preference?

No, it seems they are static. An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.

Can I disable intent filter verification service?

In conclusion, the Intent Filter Verification Service system app on Android is a security feature that verifies the authenticity of apps that are trying to access your data. If you're not comfortable with this app having access to your data, you can disable it in your device's settings.

What is intent filter in Android manifest?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What is the difference between intent and intent filter?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.


2 Answers

You can do this (disable the intent filter from android manifest programatically) by using Activity Alias:

1) add (e.g. AliasMySocialNetworkActivity) in AndroidManifest.xml to your MySocialNetworkActivity and move your intent-filter to them. It will be looks like that:

         <activity-alias
            android:targetActivity=".MySocialNetworkActivity"
            android:name=".AliasMySocialNetworkActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.PROCESS_TEXT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
             </intent-filter>
        </activity-alias>

2) add this code to suppress intent-filter in alias activity when You need that

PackageManager pm = getApplicationContext().getPackageManager();
        ComponentName compName =
                new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
        pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

3) restore intent-filter, when You need it:

PackageManager pm = getApplicationContext().getPackageManager();
    ComponentName compName =
            new ComponentName(getPackageName(), getPackageName() + ".AliasMySocialNetworkActivity");
    pm.setComponentEnabledSetting(
            compName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

For more details see this or that answer.

Update

Actually You don't need Alias, just use PackageManager.COMPONENT_ENABLED_STATE_DISABLED/PackageManager.COMPONENT_ENABLED_STATE_ENABLED

PackageManager pm = getApplicationContext().getPackageManager();
        ComponentName compName =
                new ComponentName(getPackageName(), getPackageName() + ".MySocialNetworkActivity");
        pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

directly on your activity. Thanks @pskink.

like image 93
Andrii Omelchenko Avatar answered Sep 19 '22 17:09

Andrii Omelchenko


In my case,

Code in AndroidManifest.xml

<activity-alias
        android:name="alias.Shortcut"
        android:icon="@drawable/shortcut_icon"
        android:label="@string/shortcut_text"
        android:targetActivity="com.myexample.shortcutexample.ShortcutActivity">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

Code to disable alias

PackageManager pm = getApplicationContext().getPackageManager();
        ComponentName compName =
                new ComponentName(getPackageName(), getPackageName() + ".alias.Shortcut");
        pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

when I tried

getPackageName() + ".alias.Shortcut"

I got java.lang.IllegalArgumentException: Component class com.myexample.shortcutexample.alias.Shortcut does not exist in com.myexample.shortcutexample

So I changed my code to

ComponentName compName =
                new ComponentName(getPackageName(), "alias.Shortcut");

and it worked like charm.

like image 40
Abhinav Dua Avatar answered Sep 18 '22 17:09

Abhinav Dua