|
16 | 16 | package de.cketti.safecontentresolver; |
17 | 17 |
|
18 | 18 |
|
| 19 | +import android.content.Context; |
| 20 | + |
| 21 | +import com.getkeepsafe.relinker.MissingLibraryException; |
| 22 | +import com.getkeepsafe.relinker.ReLinker; |
| 23 | + |
| 24 | + |
19 | 25 | class Os { |
20 | | - static { |
21 | | - System.loadLibrary("os-compat"); |
| 26 | + private static final String LIBRARY_NAME = "os-compat"; |
| 27 | + |
| 28 | + private static Context context; |
| 29 | + private static boolean libraryNeedsLoading = true; |
| 30 | + private static UnsupportedOperationException loadFailedException; |
| 31 | + |
| 32 | + |
| 33 | + synchronized static void init(Context context) { |
| 34 | + if (context == null) { |
| 35 | + throw new NullPointerException("Argument 'context' must not be null"); |
| 36 | + } |
| 37 | + |
| 38 | + // Only get the context here. Load the library before doing the actual work (hopefully in a background thread). |
| 39 | + if (Os.context == null) { |
| 40 | + Os.context = context.getApplicationContext(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + static int fstat(int fileDescriptor) throws ErrnoException, UnsupportedOperationException { |
| 45 | + synchronized (Os.class) { |
| 46 | + if (context == null) { |
| 47 | + throw new IllegalStateException("Call Os.init(Context) before attempting to call Os.fstat()"); |
| 48 | + } |
| 49 | + |
| 50 | + if (libraryNeedsLoading) { |
| 51 | + loadLibrary(); |
| 52 | + } else if (loadFailedException != null) { |
| 53 | + throw loadFailedException; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return nativeFstat(fileDescriptor); |
| 58 | + } |
| 59 | + |
| 60 | + private static void loadLibrary() { |
| 61 | + libraryNeedsLoading = false; |
| 62 | + try { |
| 63 | + ReLinker.loadLibrary(context, LIBRARY_NAME); |
| 64 | + } catch (MissingLibraryException | UnsatisfiedLinkError e) { |
| 65 | + loadFailedException = new UnsupportedOperationException("Failed to load native library " + LIBRARY_NAME, e); |
| 66 | + throw loadFailedException; |
| 67 | + } |
22 | 68 | } |
23 | 69 |
|
24 | | - public static native int fstat(int fileDescriptor) throws ErrnoException; |
| 70 | + private static native int nativeFstat(int fileDescriptor) throws ErrnoException; |
25 | 71 | } |
0 commit comments