/*
 * Copyright (C) 2017 Hazuki
 *
 * 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.
 */

package jp.hazuki.yuzubrowser.utils;

import android.net.Uri;

import java.net.IDN;

public class UrlUtils {

    public static String decodeUrl(String url) {
        if (url == null) return null;
        return Uri.decode(decodeUrl(Uri.parse(url)).toString());
    }

    public static Uri decodeUrl(Uri uri) {
        String host = uri.getAuthority();
        if (host != null) {
            return uri.buildUpon()
                    .authority(decodePunyCode(host))
                    .build();
        } else {
            return uri;
        }
    }

    public static String decodeUrlHost(String url) {
        String host = Uri.parse(url).getHost();
        if (host != null)
            return decodePunyCode(host);
        return null;
    }

    private static String decodePunyCode(String domain) {
        return IDN.toUnicode(domain);
    }
}

@@ -649,16 +649,47 @@
 net.Uri;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -835,19 +835,8 @@
 urn 
-Uri.decode(
 deco
@@ -867,17 +867,16 @@
 String()
-)
 ;%0A    %7D%0A
@@ -931,214 +931,269 @@
     
-String host = uri.getAuthority();%0A        if (host != null) %7B%0A            return uri.buildUpon()%0A                    .authority(decodePunyCode(host))%0A                    .build();%0A        %7D else %7B%0A 
+Uri.Builder decode = uri.buildUpon();%0A        if (isValid(uri.getQuery()))%0A            decode.query(uri.getQuery());%0A        if (isValid(uri.getFragment()))%0A            decode.fragment(uri.getFragment());%0A        decode.authority(decodeAuthority(uri));%0A
         
    r
@@ -1192,32 +1192,30 @@
     
-   
 return 
-uri;%0A        %7D
+decode.build();
 %0A   
@@ -1515,11 +1515,1077 @@
 );%0A    %7D
+%0A%0A    private static String decodeAuthority(Uri uri) %7B%0A        String host = uri.getHost();%0A        if (TextUtils.isEmpty(host)) %7B%0A            return uri.getEncodedAuthority();%0A        %7D else %7B%0A            host = decodePunyCode(host);%0A        %7D%0A%0A        String userInfo = uri.getUserInfo();%0A        boolean noUserInfo = TextUtils.isEmpty(userInfo);%0A        int port = uri.getPort();%0A%0A        if (noUserInfo && port == -1)%0A            return host;%0A%0A        StringBuilder builder = new StringBuilder();%0A%0A        if (!noUserInfo) %7B%0A            if (isValid(userInfo)) %7B%0A                builder.append(userInfo).append(%22@%22);%0A            %7D else %7B%0A                builder.append(uri.getEncodedUserInfo()).append(%22@%22);%0A            %7D%0A        %7D%0A%0A        builder.append(host);%0A%0A        if (port %3E -1) %7B%0A            builder.append(%22:%22).append(port);%0A        %7D%0A%0A        return builder.toString();%0A    %7D%0A%0A    private static final char INVALID_CHAR = '%5CuFFFD';%0A%0A    private static boolean isValid(String str) %7B%0A        return str != null && !(str.indexOf(INVALID_CHAR) %3E -1);%0A    %7D
 %0A%7D%0A

fix url decoder