Debian Codegraph @

tests/AppResourcesLoaders/src/com/android/example/loaders/LoadersApplication.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.example.loaders;
18
19 import android.app.Application;
20 import android.content.res.Resources;
21 import android.content.res.loader.ResourcesLoader;
22 import android.content.res.loader.ResourcesProvider;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 public class LoadersApplication extends Application {
32 private static final String TAG = "LoadersApplication";
33 private static final String LOADER_RESOURCES_APK = "AppResourcesLoaders_Overlay.apk";
34
35 @Override
36 public void onCreate() {
37 try {
38 final Resources resources = getResources();
39 final ResourcesLoader loader = new ResourcesLoader();
40 loader.addProvider(ResourcesProvider.loadFromApk(copyResource(LOADER_RESOURCES_APK)));
41 resources.addLoaders(loader);
42 } catch (IOException e) {
43 throw new IllegalStateException("Failed to load loader resources ", e);
44 }
45 }
46
47 private ParcelFileDescriptor copyResource(String fileName) throws IOException {
48 final File apkFile = new File(getFilesDir(), fileName);
49 final InputStream is = getClassLoader().getResourceAsStream(LOADER_RESOURCES_APK);
50 final FileOutputStream os = new FileOutputStream(apkFile);
51 byte[] buffer = new byte[8192];
52 int count;
53 while ((count = is.read(buffer)) != -1) {
54 os.write(buffer, 0, count);
55 }
56 return ParcelFileDescriptor.open(apkFile, ParcelFileDescriptor.MODE_READ_ONLY);
57 }
58 }