OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.mojo_shell_apk; | 5 package org.chromium.mojo_shell_apk; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.util.Log; |
8 | 9 |
9 import org.chromium.base.JNINamespace; | 10 import org.chromium.base.JNINamespace; |
10 | 11 |
| 12 import java.io.File; |
11 import java.util.ArrayList; | 13 import java.util.ArrayList; |
12 import java.util.Arrays; | 14 import java.util.Arrays; |
13 import java.util.List; | 15 import java.util.List; |
14 | 16 |
15 /** | 17 /** |
16 * A placeholder class to call native functions. | 18 * A placeholder class to call native functions. |
17 **/ | 19 **/ |
18 @JNINamespace("mojo") | 20 @JNINamespace("mojo") |
19 public class MojoMain { | 21 public class MojoMain { |
| 22 private static final String TAG = "MojoMain"; |
| 23 |
| 24 // Directory where applications bundled with the shell will be extracted. |
| 25 private static final String LOCAL_APP_DIRECTORY = "local_apps"; |
| 26 // Individual applications bundled with the shell as assets. |
| 27 private static final String NETWORK_LIBRARY_APP = "network_service.mojo"; |
| 28 |
20 /** | 29 /** |
21 * A guard flag for calling nativeInit() only once. | 30 * A guard flag for calling nativeInit() only once. |
22 **/ | 31 **/ |
23 private static boolean sInitialized = false; | 32 private static boolean sInitialized = false; |
24 | 33 |
25 /** | 34 /** |
| 35 * Deletes directories holding the temporary files. This should be called ea
rly on shell startup |
| 36 * to clean up after the previous run. |
| 37 */ |
| 38 static void clearTemporaryFiles(Context context) { |
| 39 FileHelper.deleteRecursively(getLocalAppsDir(context)); |
| 40 } |
| 41 |
| 42 /** |
26 * Initializes the native system. | 43 * Initializes the native system. |
27 **/ | 44 **/ |
28 static void ensureInitialized(Context applicationContext, String[] parameter
s) { | 45 static void ensureInitialized(Context applicationContext, String[] parameter
s) { |
29 if (sInitialized) | 46 if (sInitialized) |
30 return; | 47 return; |
31 List<String> parametersList = new ArrayList<String>(); | 48 |
32 // Program name. | 49 try { |
33 parametersList.add("mojo_shell"); | 50 FileHelper.extractFromAssets(applicationContext, NETWORK_LIBRARY_APP
, |
34 if (parameters != null) { | 51 getLocalAppsDir(applicationContext), false); |
35 parametersList.addAll(Arrays.asList(parameters)); | 52 |
| 53 List<String> parametersList = new ArrayList<String>(); |
| 54 // Program name. |
| 55 parametersList.add("mojo_shell"); |
| 56 if (parameters != null) { |
| 57 parametersList.addAll(Arrays.asList(parameters)); |
| 58 } |
| 59 |
| 60 nativeInit(applicationContext, |
| 61 parametersList.toArray(new String[parametersList.size()]), |
| 62 getLocalAppsDir(applicationContext).getAbsolutePath()); |
| 63 sInitialized = true; |
| 64 } catch (Exception e) { |
| 65 Log.e(TAG, "MojoMain initialization failed.", e); |
| 66 throw new RuntimeException(e); |
36 } | 67 } |
37 nativeInit(applicationContext, parametersList.toArray(new String[paramet
ersList.size()])); | |
38 sInitialized = true; | |
39 } | 68 } |
40 | 69 |
41 /** | 70 /** |
42 * Starts the specified application in the specified context. | 71 * Starts the specified application in the specified context. |
43 **/ | 72 **/ |
44 static void start(final String appUrl) { | 73 static void start(final String appUrl) { |
45 nativeStart(appUrl); | 74 nativeStart(appUrl); |
46 } | 75 } |
47 | 76 |
| 77 private static File getLocalAppsDir(Context context) { |
| 78 return context.getDir(LOCAL_APP_DIRECTORY, Context.MODE_PRIVATE); |
| 79 } |
| 80 |
48 /** | 81 /** |
49 * Initializes the native system. This API should be called only once per pr
ocess. | 82 * Initializes the native system. This API should be called only once per pr
ocess. |
50 **/ | 83 **/ |
51 private static native void nativeInit(Context context, String[] parameters); | 84 private static native void nativeInit(Context context, String[] parameters, |
| 85 String bundledAppsDirectory); |
52 | 86 |
53 private static native void nativeStart(String appUrl); | 87 private static native void nativeStart(String appUrl); |
54 } | 88 } |
OLD | NEW |