1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.libraries.mobiledatadownload.file.backends; 17 18 import android.net.Uri; 19 import android.text.TextUtils; 20 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException; 21 import java.io.File; 22 23 /** 24 * Adapter for converting "file:" URIs into java.io.File. This is considered dangerous since it 25 * ignores parts of the Uri at the caller's peril, and thus is only available to whitelisted clients 26 * (mostly internal). 27 */ 28 public class FileUriAdapter implements UriAdapter { 29 30 private static final FileUriAdapter INSTANCE = new FileUriAdapter(); 31 FileUriAdapter()32 private FileUriAdapter() {} 33 instance()34 public static FileUriAdapter instance() { 35 return INSTANCE; 36 } 37 38 @Override toFile(Uri uri)39 public File toFile(Uri uri) throws MalformedUriException { 40 if (!uri.getScheme().equals("file")) { 41 throw new MalformedUriException("Scheme must be 'file'"); 42 } 43 if (!TextUtils.isEmpty(uri.getQuery())) { 44 throw new MalformedUriException("Did not expect uri to have query"); 45 } 46 if (!TextUtils.isEmpty(uri.getAuthority())) { 47 throw new MalformedUriException("Did not expect uri to have authority"); 48 } 49 return new File(uri.getPath()); 50 } 51 } 52