Java Code Examples for android.net.Uri#isRelative()

The following examples show how to use android.net.Uri#isRelative() . 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: DefaultUriAdapter.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Uri rewrite(WXSDKInstance instance, String type, Uri uri) {
  if (TextUtils.isEmpty(instance.getBundleUrl())) {
    return uri;
  }

  Uri base = Uri.parse(instance.getBundleUrl());
  Uri.Builder resultBuilder = uri.buildUpon();
  
 if (uri.isRelative()) {
    //When uri is empty, means use the base url instead. Web broswer behave this way.
    if(uri.getEncodedPath().length() == 0){
      return base;
    } else {
      resultBuilder = buildRelativeURI(resultBuilder, base, uri);
      return resultBuilder.build();
    }
  }
  return uri;
}
 
Example 2
Source File: ChanLocator.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
public final Uri validateClickedUriString(String uriString, String boardName, String threadNumber) {
	Uri uri = uriString != null ? Uri.parse(uriString) : null;
	if (uri != null && uri.isRelative()) {
		Uri baseUri = safe.createThreadUri(boardName, threadNumber);
		if (baseUri != null) {
			String query = StringUtils.nullIfEmpty(uri.getQuery());
			String fragment = StringUtils.nullIfEmpty(uri.getFragment());
			Uri.Builder builder = baseUri.buildUpon().encodedQuery(query).encodedFragment(fragment);
			String path = uri.getPath();
			if (!StringUtils.isEmpty(path)) {
				builder.encodedPath(path);
			}
			return builder.build();
		}
	}
	return uri;
}
 
Example 3
Source File: DashParser.java    From MediaPlayer-Extended with Apache License 2.0 6 votes vote down vote up
/**
 * Extends an URL with an extended path if the extension is relative, or replaces the entire URL
 * with the extension if it is absolute.
 */
private static Uri extendUrl(Uri url, String urlExtension) {
    urlExtension = urlExtension.replace(" ", "%20"); // Convert spaces

    Uri newUrl = Uri.parse(urlExtension);

    if(newUrl.isRelative()) {
        /* Uri.withAppendedPath appends the extension to the end of the "real" server path,
         * instead of the end of the uri string.
         * Example: http://server.com/foo?file=http://server2.net/ + file1.mp4
         *           => http://server.com/foo/file1.mp4?file=http://server2.net/
         * To avoid this, we need to join as strings instead. */
        newUrl = Uri.parse(url.toString() + urlExtension);
    }
    return newUrl;
}
 
Example 4
Source File: HtmlBook.java    From BookyMcBookface with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ReadPoint locateReadPoint(String section) {
    ReadPoint readPoint = new ReadPoint();
    readPoint.setId("1");

    Uri suri = Uri.parse(section);

    if (suri.isRelative()) {
        suri = new Uri.Builder().scheme("file").path(getFile().getPath()).fragment(suri.getFragment()).build();
    }

    readPoint.setPoint(suri);
    return readPoint;
}
 
Example 5
Source File: EpubBook.java    From BookyMcBookface with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ReadPoint locateReadPoint(String section) {
    ReadPoint point = null;

    if (section==null) return null;

    Uri suri = null;

    try {
        suri = Uri.parse(Uri.decode(section));
    } catch (Exception e) {
        Log.e("Epub", e.getMessage(), e);
    }

    if (suri==null) return null;

    if (suri.isRelative()) {
        suri = new Uri.Builder().scheme("file").path(getFullBookContentDir().getPath()).appendPath(suri.getPath()).fragment(suri.getFragment()).build();
    }

    String file = suri.getLastPathSegment();

    if (file==null) return null;

    String sectionID = null;

    for (Map.Entry<String,String> entry: docFiles.entrySet()) {
        if (file.equals(entry.getValue())) {
            sectionID = entry.getKey();
        }
    }

    if (sectionID!=null) {
        point = new ReadPoint();
        point.setId(sectionID);
        point.setPoint(suri);
    }

    return point;
}
 
Example 6
Source File: DefaultUriAdapter.java    From weex-uikit with MIT License 5 votes vote down vote up
@NonNull
@Override
public Uri rewrite(WXSDKInstance instance, String type, Uri uri) {
  Uri base = Uri.parse(instance.getBundleUrl());
  Uri.Builder resultBuilder = uri.buildUpon();

  if (uri.isRelative()) {
    resultBuilder = buildRelativeURI(resultBuilder, base, uri);
    return resultBuilder.build();
  }
  return uri;


}
 
Example 7
Source File: RuleSpannables.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a menu item for URLSpan. <strong>Note: </strong> This method will not create menu item
 * for relative URLs.
 */
private @Nullable ContextMenuItem createMenuItemForUrlSpan(
    Context context,
    ContextMenuItemBuilder menuItemBuilder,
    int itemId,
    Spannable spannable,
    URLSpan span) {
  final String url = span.getURL();
  final int start = spannable.getSpanStart(span);
  final int end = spannable.getSpanEnd(span);
  if (start < 0 || end < 0) {
    return null;
  }
  final CharSequence label = spannable.subSequence(start, end);
  if (TextUtils.isEmpty(url) || TextUtils.isEmpty(label)) {
    return null;
  }

  final Uri uri = Uri.parse(url);
  if (uri.isRelative()) {
    // Generally, only absolute URIs are resolvable to an activity
    return null;
  }

  // Strip out ClickableSpans/UrlSpans from the label text.
  // A11y framework has changed how it handles double-tap from O. It's possible that double-tap
  // on the menu item will invoke ClickableSpans in the label text instead of calling
  // MenuItemClickListener. Thus we should remove ClickableSpans from label text.
  // Also apply this rule to pre-O in order to have consistent text appearance.
  SpannableUtils.stripTargetSpanFromText(label, TARGET_SPAN_CLASS);
  final ContextMenuItem item =
      menuItemBuilder.createMenuItem(context, R.id.group_links, itemId, Menu.NONE, label);
  item.setOnMenuItemClickListener(new UrlSpanMenuItemClickListener(context, uri));
  return item;
}
 
Example 8
Source File: HttpClient.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
Uri obtainRedirectedUri(Uri requestedUri, String locationHeader) {
	Uri redirectedUri;
	if (!StringUtils.isEmpty(locationHeader)) {
		redirectedUri = Uri.parse(locationHeader);
		if (redirectedUri.isRelative()) {
			Uri.Builder builder = redirectedUri.buildUpon().scheme(requestedUri.getScheme())
					.authority(requestedUri.getAuthority());
			String redirectedPath = StringUtils.emptyIfNull(redirectedUri.getPath());
			if (!redirectedPath.isEmpty() && !redirectedPath.startsWith("/")) {
				String path = StringUtils.emptyIfNull(requestedUri.getPath());
				if (!path.endsWith("/")) {
					int index = path.lastIndexOf('/');
					if (index >= 0) {
						path = path.substring(0, index + 1);
					} else {
						path = "/";
					}
				}
				path += redirectedPath;
				builder.path(path);
			}
			redirectedUri = builder.build();
		}
	} else {
		redirectedUri = requestedUri;
	}
	return redirectedUri;
}
 
Example 9
Source File: ChanLocator.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
@Public
public final boolean isChanHostOrRelative(Uri uri) {
	if (uri != null) {
		if (uri.isRelative()) {
			return true;
		}
		String host = uri.getHost();
		return host != null && isChanHost(host);
	}
	return false;
}
 
Example 10
Source File: ChanLocator.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public final Uri convert(Uri uri) {
	if (uri != null) {
		String preferredScheme = getPreferredScheme();
		String host = uri.getHost();
		String preferredHost = getPreferredHost();
		boolean relative = uri.isRelative();
		boolean webScheme = isWebScheme(uri);
		Uri.Builder builder = null;
		if (relative || webScheme && isConvertableChanHost(host)) {
			if (!StringUtils.equals(host, preferredHost)) {
				if (builder == null) {
					builder = uri.buildUpon().scheme(preferredScheme);
				}
				builder.authority(preferredHost);
			}
		} else if (webScheme) {
			String hostTransition = getHostTransition(preferredHost, host);
			if (hostTransition != null) {
				if (builder == null) {
					builder = uri.buildUpon().scheme(preferredScheme);
				}
				builder.authority(hostTransition);
			}
		}
		if (StringUtils.isEmpty(uri.getScheme()) ||
				webScheme && !preferredScheme.equals(uri.getScheme()) && isChanHost(host)) {
			if (builder == null) {
				builder = uri.buildUpon().scheme(preferredScheme);
			}
			builder.scheme(preferredScheme);
		}
		if (builder != null) {
			return builder.build();
		}
	}
	return uri;
}
 
Example 11
Source File: InAppBrowser.java    From cordova-android-chromeview with Apache License 2.0 5 votes vote down vote up
/**
 * Convert relative URL to full path
 * 
 * @param url
 * @return 
 */
private String updateUrl(String url) {
    Uri newUrl = Uri.parse(url);
    if (newUrl.isRelative()) {
        url = this.webView.getUrl().substring(0, this.webView.getUrl().lastIndexOf("/")+1) + url;
    }
    return url;
}
 
Example 12
Source File: DialogPresenter.java    From kognitivo with Apache License 2.0 4 votes vote down vote up
public static void setupAppCallForWebFallbackDialog(
        AppCall appCall,
        Bundle parameters,
        DialogFeature feature) {
    Validate.hasFacebookActivity(FacebookSdk.getApplicationContext());
    Validate.hasInternetPermissions(FacebookSdk.getApplicationContext());

    String featureName = feature.name();
    Uri fallbackUrl = getDialogWebFallbackUri(feature);
    if (fallbackUrl == null) {
        throw new FacebookException(
                "Unable to fetch the Url for the DialogFeature : '" + featureName + "'");
    }

    // Since we're talking to the server here, let's use the latest version we know about.
    // We know we are going to be communicating over a bucketed protocol.
    int protocolVersion = NativeProtocol.getLatestKnownVersion();
    Bundle webParams = ServerProtocol.getQueryParamsForPlatformActivityIntentWebFallback(
            appCall.getCallId().toString(),
            protocolVersion,
            parameters);
    if (webParams == null) {
        throw new FacebookException("Unable to fetch the app's key-hash");
    }

    // Now form the Uri
    if (fallbackUrl.isRelative()) {
        fallbackUrl = Utility.buildUri(
                ServerProtocol.getDialogAuthority(),
                fallbackUrl.toString(),
                webParams);
    } else {
        fallbackUrl = Utility.buildUri(
                fallbackUrl.getAuthority(),
                fallbackUrl.getPath(),
                webParams);
    }

    Bundle intentParameters = new Bundle();
    intentParameters.putString(NativeProtocol.WEB_DIALOG_URL, fallbackUrl.toString());
    intentParameters.putBoolean(NativeProtocol.WEB_DIALOG_IS_FALLBACK, true);

    Intent webDialogIntent = new Intent();
    NativeProtocol.setupProtocolRequestIntent(
            webDialogIntent,
            appCall.getCallId().toString(),
            feature.getAction(),
            NativeProtocol.getLatestKnownVersion(),
            intentParameters);
    webDialogIntent.setClass(FacebookSdk.getApplicationContext(), FacebookActivity.class);
    webDialogIntent.setAction(FacebookDialogFragment.TAG);

    appCall.setRequestIntent(webDialogIntent);
}
 
Example 13
Source File: FileUtils.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public static Uri catUri(final Context context, final Uri folder, final String file)
{
    if (file == null)
    {
        return null;
    }

    final Uri parsedFile = Uri.parse(file);
    if (folder == null || !parsedFile.isRelative())
    {
        return parsedFile;
    }

    final String[] nameParts = file.split("/", -1);
    Uri resUri = folder;
    for (String part : nameParts)
    {
        if (part == null || resUri == null)
        {
            break;
        }
        Uri newUri = null;
        if (AdapterBaseImpl.PLS.equals(part))
        {
            if (FileUtils.isContentUri(resUri))
            {
                newUri = AdapterDocuments.getParent(resUri);
            }
            else
            {
                newUri = FileUtils.getParentDirectory(resUri);
            }
        }
        else
        {
            if (FileUtils.isContentUri(resUri))
            {
                newUri = AdapterDocuments.withAppendedPath(context, resUri, part);
            }
            else
            {
                newUri = Uri.withAppendedPath(resUri, part);
            }
        }
        if (newUri != null)
        {
            resUri = newUri;
        }
    }
    return resUri;
}
 
Example 14
Source File: WebContent.java    From deagle with Apache License 2.0 4 votes vote down vote up
public static boolean view(final Context context, final String url) {
	Uri uri = Uri.parse(url);
	if (uri.isRelative()) uri = Uri.parse("http://" + url);		// Append http if no scheme
	return view(context, uri);
}