OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.webapk.shell_apk; | 5 package org.chromium.webapk.shell_apk; |
6 | 6 |
7 import android.app.Service; | 7 import android.app.Service; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.os.Bundle; |
10 import android.os.IBinder; | 11 import android.os.IBinder; |
11 import android.util.Log; | 12 import android.util.Log; |
12 | 13 |
13 import org.chromium.webapk.lib.common.WebApkUtils; | 14 import org.chromium.webapk.lib.common.WebApkUtils; |
14 | 15 |
15 import java.io.File; | 16 import java.io.File; |
16 import java.lang.reflect.Constructor; | 17 import java.lang.reflect.Constructor; |
17 | 18 |
18 /** | 19 /** |
19 * Shell class for services provided by WebAPK to Chrome. Extracts code with imp
lementation of | 20 * Shell class for services provided by WebAPK to Chrome. Extracts code with imp
lementation of |
20 * services from .dex file in Chrome APK. | 21 * services from .dex file in Chrome APK. |
21 */ | 22 */ |
22 public class WebApkServiceFactory extends Service { | 23 public class WebApkServiceFactory extends Service { |
23 private static final String TAG = "cr_WebApkServiceFactory"; | 24 private static final String TAG = "cr_WebApkServiceFactory"; |
24 | 25 |
25 /** | 26 /** |
26 * Name of the class with IBinder API implementation. | 27 * Name of the class with IBinder API implementation. |
27 */ | 28 */ |
28 private static final String WEBAPK_SERVICE_IMPL_CLASS_NAME = | 29 private static final String WEBAPK_SERVICE_IMPL_CLASS_NAME = |
29 "org.chromium.webapk.lib.runtime_library.WebApkServiceImpl"; | 30 "org.chromium.webapk.lib.runtime_library.WebApkServiceImpl"; |
30 | 31 |
| 32 private static final String KEY_SMALL_ICON_ID = "small_icon_id"; |
| 33 |
31 /* | 34 /* |
32 * ClassLoader for loading {@link WEBAPK_SERVICE_IMPL_CLASS_NAME}. Static so
that all | 35 * ClassLoader for loading {@link WEBAPK_SERVICE_IMPL_CLASS_NAME}. Static so
that all |
33 * {@link WebApkServiceFactory} service instatiations use the same ClassLoad
er during the app's | 36 * {@link WebApkServiceFactory} service instatiations use the same ClassLoad
er during the app's |
34 * lifetime. | 37 * lifetime. |
35 */ | 38 */ |
36 private static ClassLoader sClassLoader; | 39 private static ClassLoader sClassLoader; |
37 | 40 |
38 @Override | 41 @Override |
39 public IBinder onBind(Intent intent) { | 42 public IBinder onBind(Intent intent) { |
40 ClassLoader webApkClassLoader = getClassLoaderInstance(this); | 43 ClassLoader webApkClassLoader = getClassLoaderInstance(this); |
41 if (webApkClassLoader == null) { | 44 if (webApkClassLoader == null) { |
42 Log.w(TAG, "Unable to create ClassLoader."); | 45 Log.w(TAG, "Unable to create ClassLoader."); |
43 return null; | 46 return null; |
44 } | 47 } |
45 | 48 |
46 try { | 49 try { |
47 Class<?> webApkServiceImplClass = | 50 Class<?> webApkServiceImplClass = |
48 webApkClassLoader.loadClass(WEBAPK_SERVICE_IMPL_CLASS_NAME); | 51 webApkClassLoader.loadClass(WEBAPK_SERVICE_IMPL_CLASS_NAME); |
49 Constructor<?> webApkServiceImplConstructor = | 52 Constructor<?> webApkServiceImplConstructor = |
50 webApkServiceImplClass.getConstructor(Context.class, int.cla
ss); | 53 webApkServiceImplClass.getConstructor(Context.class, Bundle.
class); |
51 return (IBinder) webApkServiceImplConstructor.newInstance( | 54 Bundle bundle = new Bundle(); |
52 new Object[] {this, R.drawable.app_icon}); | 55 bundle.putInt(KEY_SMALL_ICON_ID, R.drawable.app_icon); |
| 56 return (IBinder) webApkServiceImplConstructor.newInstance(new Object
[] {this, bundle}); |
53 } catch (Exception e) { | 57 } catch (Exception e) { |
54 Log.w(TAG, "Unable to create WebApkServiceImpl."); | 58 Log.w(TAG, "Unable to create WebApkServiceImpl."); |
55 e.printStackTrace(); | 59 e.printStackTrace(); |
56 return null; | 60 return null; |
57 } | 61 } |
58 } | 62 } |
59 | 63 |
60 /** | 64 /** |
61 * Gets / creates ClassLaoder for loading {@link WEBAPK_SERVICE_IMPL_CLASS_N
AME}. | 65 * Gets / creates ClassLaoder for loading {@link WEBAPK_SERVICE_IMPL_CLASS_N
AME}. |
62 * @param context WebAPK's context. | 66 * @param context WebAPK's context. |
(...skipping 18 matching lines...) Expand all Loading... |
81 return null; | 85 return null; |
82 } | 86 } |
83 | 87 |
84 File localDexDir = context.getDir("dex", Context.MODE_PRIVATE); | 88 File localDexDir = context.getDir("dex", Context.MODE_PRIVATE); |
85 File remoteDexFile = | 89 File remoteDexFile = |
86 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), "web
_apk.dex"); | 90 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), "web
_apk.dex"); |
87 return DexLoader.load(remoteContext, "web_apk.dex", WEBAPK_SERVICE_IMPL_
CLASS_NAME, | 91 return DexLoader.load(remoteContext, "web_apk.dex", WEBAPK_SERVICE_IMPL_
CLASS_NAME, |
88 remoteDexFile, localDexDir); | 92 remoteDexFile, localDexDir); |
89 } | 93 } |
90 } | 94 } |
OLD | NEW |