• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
19 import android.net.Uri;
20 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException;
21 import java.io.File;
22 
23 /**
24  * Adapter for converting "android:" 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 final class GenericUriAdapter implements UriAdapter {
29 
30   private final AndroidUriAdapter androidUriAdapter;
31   private final FileUriAdapter fileUriAdapter;
32 
GenericUriAdapter(Context context)33   private GenericUriAdapter(Context context) {
34     androidUriAdapter = AndroidUriAdapter.forContext(context);
35     fileUriAdapter = FileUriAdapter.instance();
36   }
37 
forContext(Context context)38   public static GenericUriAdapter forContext(Context context) {
39     return new GenericUriAdapter(context);
40   }
41 
42   @Override
toFile(Uri uri)43   public File toFile(Uri uri) throws MalformedUriException {
44     switch (uri.getScheme()) {
45       case AndroidUri.SCHEME_NAME:
46         return androidUriAdapter.toFile(uri);
47       case FileUri.SCHEME_NAME:
48         return fileUriAdapter.toFile(uri);
49       default:
50         throw new MalformedUriException("Couldn't convert URI to path: " + uri);
51     }
52   }
53 }
54