| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
} |