From 30097c2249bdbbe6d9038124e1646380653417dc Mon Sep 17 00:00:00 2001 From: max-klaus Date: Wed, 11 Sep 2019 19:04:12 +0300 Subject: [PATCH] Fixed Huawei build --- OsmAnd/.gitignore | 5 ++ OsmAnd/AndroidManifest-huawei.xml | 40 +++++----- OsmAnd/build.gradle | 67 ++++++++++++---- .../src/net/osmand/plus/HuaweiDrmHelper.java | 77 +++++++++++++------ OsmAnd/src/net/osmand/plus/Version.java | 2 +- .../osmand/plus/activities/MapActivity.java | 2 +- 6 files changed, 130 insertions(+), 63 deletions(-) diff --git a/OsmAnd/.gitignore b/OsmAnd/.gitignore index c75beb837e..294eccd4e8 100644 --- a/OsmAnd/.gitignore +++ b/OsmAnd/.gitignore @@ -13,6 +13,11 @@ libs/it.unibo.alice.tuprolog-tuprolog-3.2.1.jar libs/commons-codec-commons-codec-1.11.jar libs/OsmAndCore_android-0.1-SNAPSHOT.jar +libs/huawei-*.jar +huaweidrmlib/ +HwDRM_SDK_* +drm_strings.xml + valgrind/ bin/ dist/ diff --git a/OsmAnd/AndroidManifest-huawei.xml b/OsmAnd/AndroidManifest-huawei.xml index df31062e74..7b62dfffcc 100644 --- a/OsmAnd/AndroidManifest-huawei.xml +++ b/OsmAnd/AndroidManifest-huawei.xml @@ -1,25 +1,25 @@ + + xmlns:tools="http://schemas.android.com/tools"> - + + + + - - - - - + + + \ No newline at end of file diff --git a/OsmAnd/build.gradle b/OsmAnd/build.gradle index b36200209f..a6948b5e9e 100644 --- a/OsmAnd/build.gradle +++ b/OsmAnd/build.gradle @@ -17,9 +17,6 @@ apply plugin: 'com.android.application' // // Less important - -def huawei = (System.getenv("APP_FEATURES") && System.getenv("APP_FEATURES").contains("+huawei")) - task printc { configurations.each { if(it.isCanBeResolved()) println it.name } } @@ -114,6 +111,9 @@ android { freecustom { manifest.srcFile "AndroidManifest-freecustom.xml" } + huawei { + manifest.srcFile "AndroidManifest-huawei.xml" + } legacy { jniLibs.srcDirs = ["libc++"] @@ -177,6 +177,10 @@ android { resConfig "en" //resConfigs "xxhdpi", "nodpi" } + huawei { + dimension "version" + applicationId "net.osmand.plus.huawei" + } // CoreVersion legacy { @@ -253,12 +257,39 @@ task downloadWorldMiniBasemap { } } -task downloadHuaweiDrm { +task downloadHuaweiDrmZip { doLast { - if (huawei) { - ant.get(src: 'https://obs.cn-north-2.myhwclouds.com/hms-ds-wf/sdk/HwDRM_SDK_2.5.2.300_ADT.zip', dest: 'HwDRM_SDK_2.5.2.300_ADT.zip', skipexisting: 'true') - ant.unzip(src: 'HwDRM_SDK_2.5.2.300_ADT.zip', dest: 'libs/') - } + ant.get(src: 'https://obs.cn-north-2.myhwclouds.com/hms-ds-wf/sdk/HwDRM_SDK_2.5.2.300_ADT.zip', dest: 'HwDRM_SDK_2.5.2.300_ADT.zip', skipexisting: 'true') + ant.unzip(src: 'HwDRM_SDK_2.5.2.300_ADT.zip', dest: 'huaweidrmlib/') + } +} + +task copyHuaweiDrmLibs(type: Copy) { + dependsOn downloadHuaweiDrmZip + from "huaweidrmlib/HwDRM_SDK_2.5.2.300_ADT/libs" + into "libs" +} + +task copyHuaweiDrmValues(type: Copy) { + dependsOn downloadHuaweiDrmZip + from "huaweidrmlib/HwDRM_SDK_2.5.2.300_ADT/res" + into "res" +} + +task downloadPrebuiltHuaweiDrm { + dependsOn copyHuaweiDrmLibs, copyHuaweiDrmValues +} + +task cleanHuaweiDrmLibs(type: Delete) { + delete "huaweidrmlib" + delete fileTree("libs").matching { + include "**/huawei-*.jar" + } +} + +task cleanHuaweiDrmValues(type: Delete) { + delete fileTree("res").matching { + include "**/drm_strings.xml" } } @@ -327,8 +358,7 @@ task collectExternalResources { updateNoTranslate, validateTranslate, copyWidgetIcons, - downloadWorldMiniBasemap, - downloadHuaweiDrm + downloadWorldMiniBasemap } // Legacy core build @@ -375,9 +405,16 @@ task cleanupDuplicatesInCore() { } } afterEvaluate { - android.applicationVariants.all { variant -> - variant.javaCompiler.dependsOn(collectExternalResources, buildOsmAndCore, cleanupDuplicatesInCore) - } + android.applicationVariants.all { variant -> + variant.javaCompiler.dependsOn(collectExternalResources, buildOsmAndCore, cleanupDuplicatesInCore) + + // Use Drm strings only for huawei flavor + if ("huawei" == variant.productFlavors[0].name) { + variant.javaCompiler.dependsOn downloadPrebuiltHuaweiDrm + } else { + variant.javaCompiler.dependsOn(cleanHuaweiDrmLibs, cleanHuaweiDrmValues) + } + } } task appStart(type: Exec) { @@ -435,7 +472,5 @@ dependencies { exclude group: 'com.android.support' } - if (huawei) { - implementation files('libs/huawei-android-drm_v2.5.2.300.jar') - } + huaweiImplementation files('libs/huawei-android-drm_v2.5.2.300.jar') } diff --git a/OsmAnd/src/net/osmand/plus/HuaweiDrmHelper.java b/OsmAnd/src/net/osmand/plus/HuaweiDrmHelper.java index 3de6dbc0e6..365078995d 100644 --- a/OsmAnd/src/net/osmand/plus/HuaweiDrmHelper.java +++ b/OsmAnd/src/net/osmand/plus/HuaweiDrmHelper.java @@ -1,39 +1,66 @@ package net.osmand.plus; import android.app.Activity; +import android.util.Log; -//import com.huawei.android.sdk.drm.Drm; -//import com.huawei.android.sdk.drm.DrmCheckCallback; +import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; public class HuaweiDrmHelper { - private Activity activity; - //private DrmCheckCallback callback; - + private static final String TAG = HuaweiDrmHelper.class.getSimpleName(); + //Copyright protection id private static final String DRM_ID = "101048021"; //Copyright protection public key private static final String DRM_PUBLIC_KEY = "e0a6c798fddfd0927bd509dfeafcef4b61c4408d7ea0ca9dfb4b7766b964f801"; - - public HuaweiDrmHelper(final Activity activity) { - this.activity = activity; - /* - callback = new DrmCheckCallback() { - @Override - public void onCheckSuccess() { } - @Override - public void onCheckFailed() { - activity.finish(); - } - }; - */ + public static void check(Activity activity) { + boolean succeed = false; + try { + final WeakReference activityRef = new WeakReference<>(activity); + Class drmCheckCallbackClass = Class.forName("com.huawei.android.sdk.drm.DrmCheckCallback"); + Object callback = java.lang.reflect.Proxy.newProxyInstance( + drmCheckCallbackClass.getClassLoader(), + new java.lang.Class[]{drmCheckCallbackClass}, + new java.lang.reflect.InvocationHandler() { + + @Override + public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) { + Activity a = activityRef.get(); + if (a != null && !a.isFinishing()) { + String method_name = method.getName(); + if (method_name.equals("onCheckSuccess")) { + // skip now + } else if (method_name.equals("onCheckFailed")) { + closeApplication(a); + } + } + return null; + } + }); + + Class drmClass = Class.forName("com.huawei.android.sdk.drm.Drm"); + Class[] partypes = new Class[]{Activity.class, String.class, String.class, String.class, drmCheckCallbackClass}; + Method check = drmClass.getMethod("check", partypes); + check.invoke(null, activity, activity.getPackageName(), DRM_ID, DRM_PUBLIC_KEY, callback); + succeed = true; + + } catch (ClassNotFoundException e) { + Log.e(TAG, "check: ", e); + } catch (NoSuchMethodException e) { + Log.e(TAG, "check: ", e); + } catch (IllegalAccessException e) { + Log.e(TAG, "check: ", e); + } catch (InvocationTargetException e) { + Log.e(TAG, "check: ", e); + } + if (!succeed) { + closeApplication(activity); + } } - - public void check() { - //Drm.check(getActivity(), getActivity().getPackageName(), DRM_ID, DRM_PUBLIC_KEY, callback); - } - - public Activity getActivity() { - return this.activity; + + private static void closeApplication(Activity activity) { + ((OsmandApplication) activity.getApplication()).closeApplicationAnywayImpl(activity, true); } } diff --git a/OsmAnd/src/net/osmand/plus/Version.java b/OsmAnd/src/net/osmand/plus/Version.java index 6a6e1bb942..14ed68b100 100644 --- a/OsmAnd/src/net/osmand/plus/Version.java +++ b/OsmAnd/src/net/osmand/plus/Version.java @@ -26,7 +26,7 @@ public class Version { } public static boolean isHuawei(OsmandApplication ctx) { - return ctx.getString(R.string.versionFeatures).contains("+huawei"); + return ctx.getPackageName().endsWith(".huawei"); } public static boolean isMarketEnabled(OsmandApplication ctx) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 70e77ca48f..9695acc3bd 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -253,7 +253,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven super.onCreate(savedInstanceState); if (Version.isHuawei(getMyApplication())) { - new HuaweiDrmHelper(this).check(); + HuaweiDrmHelper.check(this); } // Full screen is not used here // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);