Java Code Examples for android.content.IntentFilter#MalformedMimeTypeException

The following examples show how to use android.content.IntentFilter#MalformedMimeTypeException . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicManagedProfileFragment.java    From enterprise-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Enables forwarding of share intent between private account and managed profile.
 */
private void enableForwarding() {
    Activity activity = getActivity();
    if (null == activity || activity.isFinishing()) {
        return;
    }
    DevicePolicyManager manager =
            (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SEND);
        filter.addDataType("text/plain");
        filter.addDataType("image/jpeg");
        // This is how you can register an IntentFilter as allowed pattern of Intent forwarding
        manager.addCrossProfileIntentFilter(BasicDeviceAdminReceiver.getComponentName(activity),
                filter, FLAG_MANAGED_CAN_ACCESS_PARENT | FLAG_PARENT_CAN_ACCESS_MANAGED);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: WifiNetworkActivity.java    From WiFiKeyShare with GNU General Public License v3.0 6 votes vote down vote up
private void setupForegroundDispatch() {
    /* initialize the PendingIntent to start for the dispatch */
    nfcPendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    /* initialize the IntentFilters to override dispatching for */
    nfcIntentFilters = new IntentFilter[3];
    nfcIntentFilters[0] = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    nfcIntentFilters[0].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[1] = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilters[1].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[2] = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    nfcIntentFilters[2].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        nfcIntentFilters[0].addDataType("*/*"); // Handle all MIME based dispatches.
    } catch (IntentFilter.MalformedMimeTypeException e) {
        Log.e(TAG, "setupForegroundDispatch: " + e.getMessage());
    }

    /* Initialize the tech lists used to perform matching for dispatching of the
     * ACTION_TECH_DISCOVERED intent */
    nfcTechLists = new String[][] {};
}
 
Example 3
Source File: MainActivity.java    From QuickLyric with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("application/lyrics");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            return;
        }
        IntentFilter[] intentFiltersArray = new IntentFilter[]{ndef,};
        try {
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);
        } catch (Exception ignored) {
        }
    }
    updatePrefsChanges();
    AppBarLayout appBarLayout = findViewById(id.appbar);
    appBarLayout.removeOnOffsetChangedListener(this);
    appBarLayout.addOnOffsetChangedListener(this);
}
 
Example 4
Source File: BasicManagedProfileFragment.java    From android-BasicManagedProfile with Apache License 2.0 6 votes vote down vote up
/**
 * Enables forwarding of share intent between private account and managed profile.
 */
private void enableForwarding() {
    Activity activity = getActivity();
    if (null == activity || activity.isFinishing()) {
        return;
    }
    DevicePolicyManager manager =
            (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SEND);
        filter.addDataType("text/plain");
        filter.addDataType("image/jpeg");
        // This is how you can register an IntentFilter as allowed pattern of Intent forwarding
        manager.addCrossProfileIntentFilter(BasicDeviceAdminReceiver.getComponentName(activity),
                filter, FLAG_MANAGED_CAN_ACCESS_PARENT | FLAG_PARENT_CAN_ACCESS_MANAGED);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: ActivityAddFriend.java    From ploggy with GNU General Public License v3.0 6 votes vote down vote up
private void setupForegroundDispatch() {
    NfcAdapter nfcAdapter = getNfcAdapter();
    if (nfcAdapter != null) {
        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            intentFilter.addDataType(getNfcMimeType());
        } catch (IntentFilter.MalformedMimeTypeException e) {
            Log.addEntry(LOG_TAG, e.getMessage());
            return;
        }
        nfcAdapter.enableForegroundDispatch(
               this,
               PendingIntent.getActivity(
                       this,
                       0,
                       new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0),
               new IntentFilter[] {intentFilter},
               null);
    }
}
 
Example 6
Source File: NfcDataWriteActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void init() {
    Intent nfcIntent = new Intent(this, this.getClass());
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nfcIntent, 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");    /* Handles all MIME based dispatches.
                                   You should specify only the ones that you need. */
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    IntentFilter[] intentFiltersArray = new IntentFilter[]{ndef,};
    String[][] techList = new String[1][1];
    techList[0][0] = MifareClassic.class.getName();
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
    disposable.add(
            nfcManager.requestProgressProcessor().onBackpressureBuffer()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            message -> binding.currentMessage.setText(message),
                            Timber::e
                    )
    );

    disposable.add(
            nfcManager.requestInitProcessor()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(init -> {
                                if (!init) {
                                    binding.nfcBg.animate().scaleX(0.1f).scaleY(0.1f).setDuration(1500)
                                            .withEndAction(() -> binding.nfcBg.setVisibility(View.GONE));
                                }
                            },
                            Timber::e)
    );
}
 
Example 7
Source File: AppRunnerActivity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void initializeNFC() {

        if (nfcInit == false) {
            PackageManager pm = getPackageManager();
            nfcSupported = pm.hasSystemFeature(PackageManager.FEATURE_NFC);

            if (nfcSupported == false) {
                return;
            }

            // when is in foreground
            MLog.d(TAG, "starting NFC");
            mAdapter = NfcAdapter.getDefaultAdapter(this);

            // PedingIntent will be delivered to this activity
            mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

            // Setup an intent filter for all MIME based dispatches
            IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
            try {
                ndef.addDataType("*/*");
            } catch (IntentFilter.MalformedMimeTypeException e) {
                throw new RuntimeException("fail", e);
            }
            mFilters = new IntentFilter[]{ndef,};

            // Setup a tech list for all NfcF tags
            mTechLists = new String[][]{new String[]{NfcF.class.getName()}};
            nfcInit = true;
        }
    }
 
Example 8
Source File: IslandProvisioning.java    From island with Apache License 2.0 5 votes vote down vote up
private static void enableAdditionalForwarding(final DevicePolicies policies) {
	final int FLAGS_BIDIRECTIONAL = FLAG_MANAGED_CAN_ACCESS_PARENT | FLAG_PARENT_CAN_ACCESS_MANAGED;
	// For sharing across Island (bidirectional)
	policies.addCrossProfileIntentFilter(new IntentFilter(ACTION_SEND), FLAGS_BIDIRECTIONAL);		// Keep for historical compatibility reason
	try {
		policies.addCrossProfileIntentFilter(IntentFilters.forAction(ACTION_SEND).withDataType("*/*"), FLAGS_BIDIRECTIONAL);
		policies.addCrossProfileIntentFilter(IntentFilters.forAction(ACTION_SEND_MULTIPLE).withDataType("*/*"), FLAGS_BIDIRECTIONAL);
	} catch (final IntentFilter.MalformedMimeTypeException ignored) {}
	// For web browser
	policies.addCrossProfileIntentFilter(IntentFilters.forAction(ACTION_VIEW).withCategory(CATEGORY_BROWSABLE).withDataSchemes("http", "https", "ftp"),
			FLAG_PARENT_CAN_ACCESS_MANAGED);
}
 
Example 9
Source File: PatientsList.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public IntentFilter buildFilter() {
    Log.i(TAG, "buildFilter()");
    IntentFilter filter = new IntentFilter(Response.RESPONSE);
    filter.addDataScheme(Subjects.CONTENT_URI.getScheme());
    try {

        filter.addDataType(Subjects.CONTENT_TYPE);
        filter.addDataType(Subjects.CONTENT_ITEM_TYPE);
    } catch (IntentFilter.MalformedMimeTypeException e) {

    }
    return filter;
}
 
Example 10
Source File: Utility.java    From Shelter with Do What The F*ck You Want To Public License 4 votes vote down vote up
public static void enforceWorkProfilePolicies(Context context) {
    DevicePolicyManager manager = context.getSystemService(DevicePolicyManager.class);
    ComponentName adminComponent = new ComponentName(context.getApplicationContext(), ShelterDeviceAdminReceiver.class);

    // Hide this app in the work profile
    context.getPackageManager().setComponentEnabledSetting(
            new ComponentName(context.getApplicationContext(), MainActivity.class),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

    // Clear everything first to ensure our policies are set properly
    manager.clearCrossProfileIntentFilters(adminComponent);

    // Allow cross-profile intents for START_SERVICE
    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.START_SERVICE),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.TRY_START_SERVICE),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.UNFREEZE_AND_LAUNCH),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.FREEZE_ALL_IN_LIST),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.PUBLIC_FREEZE_ALL),
            DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED); // Used by FreezeService in profile

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.FINALIZE_PROVISION),
            DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.START_FILE_SHUTTLE),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.START_FILE_SHUTTLE_2),
            DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);

    manager.addCrossProfileIntentFilter(
            adminComponent,
            new IntentFilter(DummyActivity.SYNCHRONIZE_PREFERENCE),
            DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);

    // Allow ACTION_SEND and ACTION_SEND_MULTIPLE to cross from managed to parent
    // TODO: Make this configurable
    IntentFilter actionSendFilter = new IntentFilter();
    actionSendFilter.addAction(Intent.ACTION_SEND);
    actionSendFilter.addAction(Intent.ACTION_SEND_MULTIPLE);
    try {
        actionSendFilter.addDataType("*/*");
    } catch (IntentFilter.MalformedMimeTypeException ignored) {
        // WTF?
    }
    actionSendFilter.addCategory(Intent.CATEGORY_DEFAULT);
    manager.addCrossProfileIntentFilter(
            adminComponent,
            actionSendFilter,
            DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
    
    // Browser intents are allowed from work profile to parent
    // TODO: Make this configurable, just as ALLOW_PARENT_PROFILE_APP_LINKING in the next function
    IntentFilter i = new IntentFilter(Intent.ACTION_VIEW);
    i.addCategory(Intent.CATEGORY_BROWSABLE);
    i.addDataScheme("http");
    i.addDataScheme("https");
    manager.addCrossProfileIntentFilter(
            adminComponent,
            i,
            DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);

    manager.setProfileEnabled(adminComponent);
}
 
Example 11
Source File: XmlHandler.java    From springreplugin with Apache License 2.0 4 votes vote down vote up
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    super.endElement(uri, localName, qName);

    switch (qName) {
        case "intent-filter":
            if (curActions != null) {
                for (String action : curActions) {
                    curFilter.addAction(action);
                }
            }
            if (curCategories != null) {
                for (String cate : curCategories) {
                    curFilter.addCategory(cate);
                }
            }

            if (curDataBeans != null) {
                for (DataBean bean : curDataBeans) {
                    if (!TextUtils.isEmpty(bean.scheme)) {
                        curFilter.addDataScheme(bean.scheme);
                    }

                    if (!TextUtils.isEmpty(bean.host) && !TextUtils.isEmpty(bean.port)) {
                        curFilter.addDataAuthority(bean.host, bean.port);
                    }

                    if (!TextUtils.isEmpty(bean.path)) {
                        curFilter.addDataPath(bean.path, bean.getPatternMatcherType());
                    }

                    try {
                        if (!TextUtils.isEmpty(bean.mimeType)) {
                            curFilter.addDataType(bean.mimeType);
                        }
                    } catch (IntentFilter.MalformedMimeTypeException e) {
                        e.printStackTrace();
                    }
                }
            }

            curActions = null;
            curCategories = null;
            curDataBeans = null;
            break;
        case "activity":
            activities.add(curComponent);
            break;
        case "service":
            services.add(curComponent);
            break;
        case "receiver":
            receivers.add(curComponent);
            break;
    }
}