Example usage for android.content IntentFilter addDataAuthority

List of usage examples for android.content IntentFilter addDataAuthority

Introduction

In this page you can find the example usage for android.content IntentFilter addDataAuthority.

Prototype

public final void addDataAuthority(String host, String port) 

Source Link

Document

Add a new Intent data authority to match against.

Usage

From source file:org.fdroid.fdroid.net.DownloaderService.java

/**
 * Get a prepared {@link IntentFilter} for use for matching this service's action events.
 *
 * @param urlString The full file URL to match.
 * @param action    {@link Downloader#ACTION_STARTED}, {@link Downloader#ACTION_PROGRESS},
 *                  {@link Downloader#ACTION_INTERRUPTED}, or {@link Downloader#ACTION_COMPLETE},
 * @return/*from   w w  w.j  av a2s . c o m*/
 */
public static IntentFilter getIntentFilter(String urlString, String action) {
    Uri uri = Uri.parse(urlString);
    IntentFilter intentFilter = new IntentFilter(action);
    intentFilter.addDataScheme(uri.getScheme());
    intentFilter.addDataAuthority(uri.getHost(), String.valueOf(uri.getPort()));
    intentFilter.addDataPath(uri.getPath(), PatternMatcher.PATTERN_LITERAL);
    return intentFilter;
}

From source file:org.fdroid.fdroid.installer.Installer.java

/**
 * Gets an {@link IntentFilter} for matching events from the install
 * process based on the original download URL as a {@link Uri}.
 *//*from   w  w w .  j  a  v a2  s . c om*/
public static IntentFilter getInstallIntentFilter(Uri uri) {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Installer.ACTION_INSTALL_STARTED);
    intentFilter.addAction(Installer.ACTION_INSTALL_COMPLETE);
    intentFilter.addAction(Installer.ACTION_INSTALL_INTERRUPTED);
    intentFilter.addAction(Installer.ACTION_INSTALL_USER_INTERACTION);
    intentFilter.addDataScheme(uri.getScheme());
    intentFilter.addDataAuthority(uri.getHost(), String.valueOf(uri.getPort()));
    intentFilter.addDataPath(uri.getPath(), PatternMatcher.PATTERN_LITERAL);
    return intentFilter;
}

From source file:uk.ac.ucl.excites.sapelli.relay.BackgroundService.java

/**
 * Register an SMS Data (Binary) Receiver
 *///from  w  ww. j av a2 s .com
private void registerSmsReceiver() {
    smsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Debug.i("Received Binary SMS");

            Bundle bundle = intent.getExtras();
            SmsMessage[] msgs = null;

            if (null != bundle) {
                // In telecommunications the term (PDU) means protocol data
                // unit.
                // There are two ways of sending and receiving SMS messages:
                // by text mode and by PDU (protocol description unit) mode.
                // The PDU string contains not only the message, but also a
                // lot of meta-information about the sender, his SMS service
                // center, the time stamp etc
                // It is all in the form of hexa-decimal octets or decimal
                // semi-octets.
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];

                for (int i = 0; i < msgs.length; i++) {
                    // Create the Message
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    // Get Message Parameters
                    SmsObject receivedSms = new SmsObject();
                    receivedSms.setTelephoneNumber(msgs[i].getOriginatingAddress());
                    receivedSms.setMessageTimestamp(msgs[i].getTimestampMillis());
                    receivedSms.setMessageData(Base64.encodeToString(msgs[i].getUserData(), Base64.CRLF));

                    Debug.d("Received SMS and it's content hash is: " + BinaryHelpers
                            .toHexadecimealString(Hashing.getMD5Hash(msgs[i].getUserData()).toByteArray()));

                    // Store the SmsObject to the db
                    dao.storeSms(receivedSms);
                }
            }

            // This will stop the Broadcast and not allow the message to
            // be interpreted by the default Android app or other apps
            abortBroadcast();
        }
    };

    // Set up the Receiver Parameters
    IntentFilter mIntentFilter = new IntentFilter();
    mIntentFilter.setPriority(999);
    mIntentFilter.addAction("android.intent.action.DATA_SMS_RECEIVED");
    mIntentFilter.addDataScheme("sms");
    // Set the Port that is listening to
    mIntentFilter.addDataAuthority("*", "2013");
    // mIntentFilter.addDataType(type)
    registerReceiver(smsReceiver, mIntentFilter);
    Debug.d("Set up BinarySMS receiver.");
}

From source file:Main.java

private static final boolean initIntentFilterFromXml(IntentFilter inf, XmlPullParser xpp) {
    try {// ww w .  j a v a2s  .co  m
        int outerDepth = xpp.getDepth();
        int type;
        final String NAME = "name";
        while ((type = xpp.next()) != XmlPullParser.END_DOCUMENT
                && (type != XmlPullParser.END_TAG || xpp.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT)
                continue;
            String tag = xpp.getName();
            if (tag.equals("action")) {
                String name = xpp.getAttributeValue(null, NAME);
                if (name != null)
                    inf.addAction(name);
            } else if (tag.equals("category")) {
                String name = xpp.getAttributeValue(null, NAME);
                if (name != null)
                    inf.addCategory(name);
            } else if (tag.equals("data")) {
                int na = xpp.getAttributeCount();
                for (int i = 0; i < na; i++) {
                    String port = null;
                    String an = xpp.getAttributeName(i);
                    String av = xpp.getAttributeValue(i);
                    if ("mimeType".equals(an)) {
                        try {
                            inf.addDataType(av);
                        } catch (MalformedMimeTypeException e) {
                        }
                    } else if ("scheme".equals(an)) {
                        inf.addDataScheme(av);
                    } else if ("host".equals(an)) {
                        inf.addDataAuthority(av, port);
                    } else if ("port".equals(an)) {
                        port = av;
                    } else if ("path".equals(an)) {
                        inf.addDataPath(av, PatternMatcher.PATTERN_LITERAL);
                    } else if ("pathPrefix".equals(an)) {
                        inf.addDataPath(av, PatternMatcher.PATTERN_PREFIX);
                    } else if ("pathPattern".equals(an)) {
                        inf.addDataPath(av, PatternMatcher.PATTERN_SIMPLE_GLOB);
                    }
                }
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.linkdroid.BroadcastReceiverService.java

private void enableIntentFilterAtCursor(Cursor c) throws MalformedMimeTypeException {
    final IntentFilter intentFilter = new IntentFilter();
    final String intentStr = c.getString(c.getColumnIndexOrThrow(ACTION));
    intentFilter.addAction(intentStr);/*from  w w  w.j ava2s .c o m*/
    final String categoryStr = c.getString(c.getColumnIndexOrThrow(CATEGORY));
    if (StringUtils.isBlank(categoryStr)) {
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    } else {
        intentFilter.addCategory(categoryStr);
    }
    final String dataAuthHostStr = c.getString(c.getColumnIndexOrThrow(DATA_AUTHORITY_HOST));
    final String dataAuthPortStr = c.getString(c.getColumnIndexOrThrow(DATA_AUTHORITY_PORT));
    if (!StringUtils.isBlank(dataAuthHostStr)) {
        intentFilter.addDataAuthority(dataAuthHostStr, dataAuthPortStr);
    }
    final String dataSchemeStr = c.getString(c.getColumnIndexOrThrow(DATA_SCHEME));
    if (!StringUtils.isBlank(dataSchemeStr)) {
        intentFilter.addDataScheme(dataSchemeStr);
    }
    final String dataTypeStr = c.getString(c.getColumnIndexOrThrow(DATA_TYPE));
    if (!StringUtils.isBlank(dataTypeStr)) {
        intentFilter.addDataType(dataTypeStr);
    }
    registerReceiver(receiver, intentFilter);
    Log.d(TAG, "registered intent filter " + intentStr);
}

From source file:com.compal.telephonytest.TelephonyTest.java

public void prepareSMS() {

    IntentFilter sendIntentFilter = new IntentFilter(SMS_SEND_ACTION);
    IntentFilter deliveryIntentFilter = new IntentFilter(SMS_DELIVERY_ACTION);
    IntentFilter dataSmsReceivedIntentFilter = new IntentFilter(DATA_SMS_RECEIVED_ACTION);

    mSendIntent = new Intent(SMS_SEND_ACTION);
    mDeliveryIntent = new Intent(SMS_DELIVERY_ACTION);

    dataSmsReceivedIntentFilter.addDataScheme("sms");
    dataSmsReceivedIntentFilter.addDataAuthority("localhost", port.toString());

    context.registerReceiver(SmsBroadcastReceiver, sendIntentFilter);
    context.registerReceiver(SmsBroadcastReceiver, deliveryIntentFilter);
    context.registerReceiver(SmsBroadcastReceiver, dataSmsReceivedIntentFilter);

    mText = mText + "_" + Long.toString(System.currentTimeMillis());

    PendingIntent mSentIntent = PendingIntent.getBroadcast(getInstrumentation().getTargetContext(), 0,
            mSendIntent, PendingIntent.FLAG_ONE_SHOT);
    PendingIntent mDeliveredIntent = PendingIntent.getBroadcast(getInstrumentation().getTargetContext(), 0,
            mDeliveryIntent, PendingIntent.FLAG_ONE_SHOT);

    byte[] data = mText.getBytes();
    mSmsManager.sendDataMessage(mPhoneNumber, null, port, data, mSentIntent, mDeliveredIntent);

}

From source file:com.java2s.intents4.IntentsDemo4Activity.java

private IntentFilter createFilterFromEditTextFields() {
    IntentFilter filter = new IntentFilter();

    if (filterActionsLayout != null) {
        int count = filterActionsLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            String action = ((EditText) ((ViewGroup) filterActionsLayout.getChildAt(i)).getChildAt(1)).getText()
                    .toString().trim();//  w w  w.  j  a  v a  2  s .c  om
            if (action.length() != 0) {
                filter.addAction(action);
            }
        }
    }

    if (filterSchemeLayout != null) {
        int count = filterSchemeLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            String scheme = ((EditText) ((ViewGroup) filterSchemeLayout.getChildAt(i)).getChildAt(1)).getText()
                    .toString().trim();
            if (scheme.length() != 0) {
                filter.addDataScheme(scheme);
            }
        }
    }

    if (filterAuthLayout != null) {
        int count = filterAuthLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            String auth = ((EditText) ((ViewGroup) filterAuthLayout.getChildAt(i)).getChildAt(1)).getText()
                    .toString().trim();
            if (auth.length() != 0) {
                Scanner scanner = new Scanner(auth);
                scanner.useDelimiter(":");
                String host = null;
                String port = null;
                if (scanner.hasNext()) {
                    host = scanner.next();
                }
                if (scanner.hasNext()) {
                    port = scanner.next();
                }
                filter.addDataAuthority(host, port);
            }
        }
    }

    if (filterPathLayout != null) {
        int count = filterPathLayout.getChildCount();
        for (int i = 0; i < count; i++) {

            ViewGroup group = (ViewGroup) filterPathLayout.getChildAt(i);
            String path = ((EditText) group.getChildAt(1)).getText().toString().trim();
            String pattern = ((TextView) ((ViewGroup) group.getChildAt(2)).getChildAt(0)).getText().toString()
                    .trim(); // ((TextView)

            int patternInt = 0;
            if (pattern.equals(pathPatterns[0])) {
                patternInt = PatternMatcher.PATTERN_LITERAL;
            }
            if (pattern.equals(pathPatterns[1])) {
                patternInt = PatternMatcher.PATTERN_PREFIX;
            }
            if (pattern.equals(pathPatterns[2])) {
                patternInt = PatternMatcher.PATTERN_SIMPLE_GLOB;
            }
            if (path.length() != 0) {
                filter.addDataPath(path, patternInt);
            }
        }
    }

    if (filterTypeLayout != null) {
        int count = filterTypeLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            String aType = ((EditText) ((ViewGroup) filterTypeLayout.getChildAt(i)).getChildAt(1)).getText()
                    .toString().trim();
            if (aType.length() != 0) {
                try {
                    filter.addDataType(aType);
                } catch (MalformedMimeTypeException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    if (filterCategoriesLayout != null) {
        int count = filterCategoriesLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            String cat = ((EditText) ((ViewGroup) filterCategoriesLayout.getChildAt(i)).getChildAt(1)).getText()
                    .toString().trim();
            if (cat.length() != 0) {
                filter.addCategory(cat);
            }
        }
    }
    return filter;
}

From source file:com.tasomaniac.openwith.resolver.ResolverActivity.java

protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {

    final ChooserHistory history = getHistory();

    if ((mAlwaysUseOption || mAdapter.hasFilteredItem()) && mAdapter.mOrigResolveList != null) {
        // Build a reasonable intent filter, based on what matched.
        IntentFilter filter = new IntentFilter();

        if (intent.getAction() != null) {
            filter.addAction(intent.getAction());
        }//from   ww w  . j  a v a 2s. c  o m
        Set<String> categories = intent.getCategories();
        if (categories != null) {
            for (String cat : categories) {
                filter.addCategory(cat);
            }
        }
        filter.addCategory(Intent.CATEGORY_DEFAULT);

        int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
        Uri data = intent.getData();
        if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
            String mimeType = intent.resolveType(this);
            if (mimeType != null) {
                try {
                    filter.addDataType(mimeType);
                } catch (IntentFilter.MalformedMimeTypeException e) {
                    Log.w("ResolverActivity", e);
                    filter = null;
                }
            }
        }
        if (filter != null && data != null && data.getScheme() != null) {
            // We need the data specification if there was no type,
            // OR if the scheme is not one of our magical "file:"
            // or "content:" schemes (see IntentFilter for the reason).
            if (cat != IntentFilter.MATCH_CATEGORY_TYPE
                    || (!"file".equals(data.getScheme()) && !"content".equals(data.getScheme()))) {
                filter.addDataScheme(data.getScheme());

                // Look through the resolved filter to determine which part
                // of it matched the original Intent.
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();

                    if (pIt != null) {
                        String ssp = data.getSchemeSpecificPart();
                        while (ssp != null && pIt.hasNext()) {
                            PatternMatcher p = pIt.next();
                            if (p.match(ssp)) {
                                filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
                                break;
                            }
                        }
                    }
                }
                Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
                if (aIt != null) {
                    while (aIt.hasNext()) {
                        IntentFilter.AuthorityEntry a = aIt.next();
                        if (a.match(data) >= 0) {
                            int port = a.getPort();
                            filter.addDataAuthority(a.getHost(), port >= 0 ? Integer.toString(port) : null);
                            break;
                        }
                    }
                }
                Iterator<PatternMatcher> pIt = ri.filter.pathsIterator();
                if (pIt != null) {
                    String path = data.getPath();
                    while (path != null && pIt.hasNext()) {
                        PatternMatcher p = pIt.next();
                        if (p.match(path)) {
                            filter.addDataPath(p.getPath(), p.getType());
                            break;
                        }
                    }
                }
            }
        }

        if (filter != null) {
            ContentValues values = new ContentValues(3);
            values.put(HOST, mRequestedUri.getHost());
            values.put(COMPONENT, intent.getComponent().flattenToString());

            if (alwaysCheck) {
                values.put(PREFERRED, true);
            }
            values.put(LAST_CHOSEN, true);
            getContentResolver().insert(CONTENT_URI, values);

            history.add(intent.getComponent().getPackageName());
        }
    }

    if (intent != null) {
        startActivity(intent);
    }
    history.save(this);
}