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.accounts.Account; 19 import android.content.Context; 20 import android.net.Uri; 21 import android.os.Build; 22 import android.text.TextUtils; 23 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException; 24 import java.io.File; 25 import java.util.ArrayList; 26 import java.util.concurrent.ExecutionException; 27 import javax.annotation.Nullable; 28 29 /** 30 * Adapter for converting "android:" URIs into java.io.File. This is considered dangerous since it 31 * ignores parts of the Uri at the caller's peril, and thus is only available to whitelisted clients 32 * (mostly internal). 33 */ 34 public final class AndroidUriAdapter implements UriAdapter { 35 36 private final Context context; 37 @Nullable private final AccountManager accountManager; 38 AndroidUriAdapter(Context context, @Nullable AccountManager accountManager)39 private AndroidUriAdapter(Context context, @Nullable AccountManager accountManager) { 40 this.context = context; 41 this.accountManager = accountManager; 42 } 43 44 /** This adapter will fail on "managed" URIs (see {@link forContext(Context, AccountManager)}). */ forContext(Context context)45 public static AndroidUriAdapter forContext(Context context) { 46 return new AndroidUriAdapter(context, /* accountManager= */ null); 47 } 48 49 /** A non-null {@code accountManager} is required to handle "managed" paths. */ forContext(Context context, AccountManager accountManager)50 public static AndroidUriAdapter forContext(Context context, AccountManager accountManager) { 51 return new AndroidUriAdapter(context, accountManager); 52 } 53 54 /* @throws MalformedUriException if the uri is not valid. */ validate(Uri uri)55 public static void validate(Uri uri) throws MalformedUriException { 56 if (!uri.getScheme().equals(AndroidUri.SCHEME_NAME)) { 57 throw new MalformedUriException("Scheme must be 'android'"); 58 } 59 if (uri.getPathSegments().isEmpty()) { 60 throw new MalformedUriException( 61 String.format("Path must start with a valid logical location: %s", uri)); 62 } 63 if (!TextUtils.isEmpty(uri.getQuery())) { 64 throw new MalformedUriException("Did not expect uri to have query"); 65 } 66 } 67 68 @Override toFile(Uri uri)69 public File toFile(Uri uri) throws MalformedUriException { 70 validate(uri); 71 ArrayList<String> pathSegments = new ArrayList<>(uri.getPathSegments()); // allow modification 72 File rootLocation; 73 switch (pathSegments.get(0)) { 74 case AndroidUri.DIRECT_BOOT_FILES_LOCATION: 75 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 76 rootLocation = context.createDeviceProtectedStorageContext().getFilesDir(); 77 } else { 78 throw new MalformedUriException( 79 String.format( 80 "Direct boot only exists on N or greater: current SDK %s", 81 Build.VERSION.SDK_INT)); 82 } 83 84 break; 85 case AndroidUri.DIRECT_BOOT_CACHE_LOCATION: 86 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 87 rootLocation = context.createDeviceProtectedStorageContext().getCacheDir(); 88 } else { 89 throw new MalformedUriException( 90 String.format( 91 "Direct boot only exists on N or greater: current SDK %s", 92 Build.VERSION.SDK_INT)); 93 } 94 95 break; 96 case AndroidUri.FILES_LOCATION: 97 rootLocation = AndroidFileEnvironment.getFilesDirWithPreNWorkaround(context); 98 break; 99 case AndroidUri.CACHE_LOCATION: 100 rootLocation = context.getCacheDir(); 101 break; 102 case AndroidUri.MANAGED_LOCATION: 103 File filesDir = AndroidFileEnvironment.getFilesDirWithPreNWorkaround(context); 104 rootLocation = new File(filesDir, AndroidUri.MANAGED_FILES_DIR_SUBDIRECTORY); 105 106 // Transform account segment from logical (plaintext) to physical (integer) representation. 107 if (pathSegments.size() >= 3) { 108 Account account; 109 try { 110 account = AccountSerialization.deserialize(pathSegments.get(2)); 111 } catch (IllegalArgumentException e) { 112 throw new MalformedUriException(e); 113 } 114 if (!AccountSerialization.isSharedAccount(account)) { 115 if (accountManager == null) { 116 throw new MalformedUriException("AccountManager cannot be null"); 117 } 118 // Blocks on disk IO to read account table. 119 try { 120 int accountId = accountManager.getAccountId(account).get(); 121 pathSegments.set(2, Integer.toString(accountId)); 122 } catch (InterruptedException e) { 123 Thread.currentThread().interrupt(); 124 throw new MalformedUriException(e); 125 } catch (ExecutionException e) { 126 // TODO(b/115940396): surface bad account as FileNotFoundException (change signature?) 127 throw new MalformedUriException(e.getCause()); 128 } 129 } 130 } 131 132 break; 133 case AndroidUri.EXTERNAL_LOCATION: 134 rootLocation = context.getExternalFilesDir(null); 135 break; 136 default: 137 throw new MalformedUriException( 138 String.format("Path must start with a valid logical location: %s", uri)); 139 } 140 return new File( 141 rootLocation, TextUtils.join(File.separator, pathSegments.subList(1, pathSegments.size()))); 142 } 143 } 144