1 /* 2 * Copyright (C) 2017 The Android Open Source Project 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 17 package com.android.phone.testapps.embmsmw; 18 19 import android.content.Context; 20 import android.net.Uri; 21 import android.telephony.mbms.FileInfo; 22 import android.telephony.mbms.FileServiceInfo; 23 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.Date; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Locale; 30 import java.util.Map; 31 import java.util.stream.Collectors; 32 33 public class FileServiceRepository { 34 private int sServiceIdCounter = 0; 35 private final Map<String, FileServiceInfo> mIdToServiceInfo = new HashMap<>(); 36 private final Map<Uri, Integer> mFileUriToResource = new HashMap<>(); 37 38 private static final String FILE_DOWNLOAD_SCHEME = "filedownload"; 39 private static final String FILE_AUTHORITY = "com.android.phone.testapps"; 40 41 private static FileServiceRepository sInstance; getInstance(Context context)42 public static FileServiceRepository getInstance(Context context) { 43 if (sInstance == null) { 44 sInstance = new FileServiceRepository(context); 45 } 46 return sInstance; 47 } 48 49 private final Context mContext; 50 FileServiceRepository(Context context)51 private FileServiceRepository(Context context) { 52 mContext = context; 53 Uri sunAndTree = initFile("sunAndTree.png", R.raw.suntree); 54 Uri snake = initFile("animals/snake.png", R.raw.snake); 55 Uri unicorn = initFile("animals/unicorn.png", R.raw.unicorn); 56 Uri sheep = initFile("animals/sheep.png", R.raw.sheep); 57 58 createFileService("Class1", sunAndTree); 59 createFileService("Class1", snake, unicorn, sheep); 60 } 61 getFileServicesForClasses( List<String> serviceClasses)62 public List<FileServiceInfo> getFileServicesForClasses( 63 List<String> serviceClasses) { 64 return mIdToServiceInfo.values().stream() 65 .filter((info) -> serviceClasses.contains(info.getServiceClassName())) 66 .collect(Collectors.toList()); 67 } 68 getAllFileServices()69 public List<FileServiceInfo> getAllFileServices() { 70 return new ArrayList<>(mIdToServiceInfo.values()); 71 } 72 getFileServiceInfoForId(String serviceId)73 public FileServiceInfo getFileServiceInfoForId(String serviceId) { 74 return mIdToServiceInfo.getOrDefault(serviceId, null); 75 } 76 getResourceForFileUri(Uri uri)77 public int getResourceForFileUri(Uri uri) { 78 return mFileUriToResource.getOrDefault(uri, 0); 79 } 80 createFileService(String className, Uri... filesIncluded)81 private void createFileService(String className, Uri... filesIncluded) { 82 sServiceIdCounter++; 83 String id = "FileServiceId[" + sServiceIdCounter + "]"; 84 List<Locale> locales = List.of(Locale.US, Locale.UK); 85 Map<Locale, String> localeDict = Map.of( 86 Locale.US, "File Source " + sServiceIdCounter, 87 Locale.UK, "File Source with extra vowels " + sServiceIdCounter); 88 List<FileInfo> fileInfos = Arrays.stream(filesIncluded) 89 .map(this::getFileInfoForUri) 90 .collect(Collectors.toList()); 91 FileServiceInfo info = new FileServiceInfo(localeDict, className, locales, 92 id, new Date(System.currentTimeMillis() - 10000), 93 new Date(System.currentTimeMillis() + 10000), 94 fileInfos); 95 mIdToServiceInfo.put(id, info); 96 } 97 initFile(String relPath, int resource)98 private Uri initFile(String relPath, int resource) { 99 Uri uri = new Uri.Builder() 100 .scheme(FILE_DOWNLOAD_SCHEME) 101 .authority(FILE_AUTHORITY) 102 .path(relPath) 103 .build(); 104 mFileUriToResource.put(uri, resource); 105 return uri; 106 } 107 getFileInfoForUri(Uri uri)108 private FileInfo getFileInfoForUri(Uri uri) { 109 if (!mFileUriToResource.containsKey(uri)) { 110 return null; 111 } 112 113 return new FileInfo(uri, "application/octet-stream"); 114 } 115 } 116