Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd02cafc authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Add versioning util

parent 215b32e0
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
  ~ Copyright 2014-2015 µg Project Team
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +14,12 @@
  ~ limitations under the License.
  -->

<manifest package="org.microg.nlp.api">
<!-- This is not needed, it's just here to stop build systems from complaining -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.microg.nlp.api">

    <application>
        <meta-data
            android:name="org.microg.nlp.API_VERSION"
            android:value="@string/nlp_api_version" />
    </application>
</manifest>
+19 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?><!--
  ~ Copyright 2014-2015 µg Project Team
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<resources>
    <string name="nlp_api_version">1</string>
</resources>
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -29,4 +29,6 @@ public class Constants {
    public static final String METADATA_BACKEND_SETTINGS_ACTIVITY = "org.microg.nlp.BACKEND_SETTINGS_ACTIVITY";
    public static final String METADATA_BACKEND_ABOUT_ACTIVITY = "org.microg.nlp.BACKEND_ABOUT_ACTIVITY";
    public static final String METADATA_BACKEND_SUMMARY = "org.microg.nlp.BACKEND_SUMMARY";
    public static final String METADATA_API_VERSION = "org.microg.nlp.API_VERSION";
    public static final String API_VERSION = "1";
}
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright 2014-2015 µg Project Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.microg.nlp.api;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class VersionUtil {

    public static String getPackageApiVersion(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo applicationInfo;
        try {
            applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
        return applicationInfo.metaData != null
                ? applicationInfo.metaData.getString(Constants.METADATA_API_VERSION)
                : null;
    }

    public static String getServiceApiVersion(Context context) {
        String apiVersion = getPackageApiVersion(context, "com.google.android.gms");
        return apiVersion == null
                ? getPackageApiVersion(context, "com.google.android.location")
                : apiVersion;
    }

    public static String getSelfApiVersion(Context context) {
        String apiVersion = getPackageApiVersion(context, context.getPackageName());
        if (apiVersion == null || !apiVersion.equals(Constants.API_VERSION)) {
            Log.w("VersionUtil", "You did not specify the currently used api version in your manifest.\n" +
                    "When using gradle + aar, this should be done automatically, if not, add the\n" +
                    "following to your <application> tag\n" +
                    "<meta-data android:name=\"" + Constants.METADATA_API_VERSION +
                    "\" android:value=\"" + Constants.API_VERSION + "\" />");
            apiVersion = Constants.API_VERSION;
        }
        return apiVersion;
    }
}