package me.thekey.android.localbroadcast;

import android.content.IntentFilter;
import android.net.Uri;
import android.os.PatternMatcher;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;

import static android.support.annotation.RestrictTo.Scope.LIBRARY;
import static me.thekey.android.localbroadcast.Constants.ACTION_ATTRIBUTES_LOADED;
import static me.thekey.android.localbroadcast.Constants.ACTION_CHANGE_DEFAULT_SESSION;
import static me.thekey.android.localbroadcast.Constants.ACTION_LOGIN;
import static me.thekey.android.localbroadcast.Constants.ACTION_LOGOUT;

public final class BroadcastUtils {
    private static final Uri URI_THEKEY = Uri.parse("thekey://");

    private static Uri theKeyUri() {
        return URI_THEKEY;
    }

    @RestrictTo(LIBRARY)
    static Uri theKeyUri(@NonNull final String guid) {
        return URI_THEKEY.buildUpon().appendPath(guid).build();
    }

    private static void addDataUri(@NonNull final IntentFilter filter, @NonNull final Uri uri, final int type) {
        final String scheme = uri.getScheme();
        if (scheme != null) {
            filter.addDataScheme(scheme);
        }
        final String host = uri.getHost();
        if (host != null) {
            filter.addDataAuthority(host, null);
        }
        final String path = uri.getPath();
        if (path != null) {
            filter.addDataPath(path, type);
        }
    }

    private static IntentFilter genericFilter(@NonNull final String action, @Nullable final String guid) {
        final IntentFilter filter = new IntentFilter(action);
        if (guid == null) {
            addDataUri(filter, theKeyUri(), PatternMatcher.PATTERN_PREFIX);
        } else {
            addDataUri(filter, theKeyUri(guid), PatternMatcher.PATTERN_LITERAL);
        }
        return filter;
    }

    public static IntentFilter loginFilter(@Nullable final String guid) {
        return genericFilter(ACTION_LOGIN, guid);
    }

    public static IntentFilter logoutFilter(@Nullable final String guid) {
        return genericFilter(ACTION_LOGOUT, guid);
    }

    public static IntentFilter changeDefaultSessionFilter() {
        return genericFilter(ACTION_CHANGE_DEFAULT_SESSION, null);
    }

    public static IntentFilter attributesLoadedFilter(@Nullable final String guid) {
        return genericFilter(ACTION_ATTRIBUTES_LOADED, guid);
    }
}

@@ -750,16 +750,29 @@
 ://%22);%0A%0A
+    @NonNull%0A
     priv
@@ -796,24 +796,24 @@
 eKeyUri() %7B%0A
-
         retu
@@ -830,24 +830,37 @@
 KEY;%0A    %7D%0A%0A
+    @NonNull%0A
     @Restric
@@ -1494,32 +1494,45 @@
        %7D%0A    %7D%0A%0A
+    @NonNull%0A
     private stat
@@ -1918,32 +1918,45 @@
  filter;%0A    %7D%0A%0A
+    @NonNull%0A
     public stati
@@ -2062,32 +2062,45 @@
 , guid);%0A    %7D%0A%0A
+    @NonNull%0A
     public stati
@@ -2208,32 +2208,45 @@
 , guid);%0A    %7D%0A%0A
+    @NonNull%0A
     public stati
@@ -2361,24 +2361,37 @@
 ll);%0A    %7D%0A%0A
+    @NonNull%0A
     public s

add annotations to several methods in BroadcastUtils