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

Commit 25f30e1d authored by Fynn Godau's avatar Fynn Godau
Browse files

Merge licensing branch into e's fork

parents d769b7a9 18732851
Loading
Loading
Loading
Loading
Loading
+55 −51
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ public abstract class LicenseChecker<D, R> {

    static final String AUTH_TOKEN_SCOPE = "oauth2:https://www.googleapis.com/auth/googleplay";

    public abstract Request<?> createRequest(String packageName, String auth, int versionCode, D data,
                                             BiConsumer<Integer, R> then, Response.ErrorListener errorListener);

    public void checkLicense(Account account, AccountManager accountManager,
                             String packageName, PackageManager packageManager,
                             RequestQueue queue, D queryData,
@@ -95,14 +98,7 @@ public abstract class LicenseChecker<D, R> {
                onResult.accept(ERROR_NON_MATCHING_UID, null);
            } else {

                accountManager.getAuthToken(
                    account, AUTH_TOKEN_SCOPE, false,
                    future -> {
                        try {
                            String auth = future.getResult().getString(KEY_AUTHTOKEN);
                            Request<?> request = createRequest(packageName, auth,
                                versionCode, queryData, (Integer integer, R r) -> {

                BiConsumer<Integer, R> onRequestFinished = (Integer integer, R r) -> {
                    try {
                        onResult.accept(integer, r);
                    } catch (RemoteException e) {
@@ -110,10 +106,20 @@ public abstract class LicenseChecker<D, R> {
                            "After telling it the license check result, remote threw an Exception.");
                        e.printStackTrace();
                    }
                                }, error -> {
                };

                Response.ErrorListener onRequestError = error -> {
                    Log.e(TAG, "license request failed with " + error.toString());
                    safeSendResult(onResult, ERROR_CONTACTING_SERVER, null);
                                });
                };

                accountManager.getAuthToken(
                    account, AUTH_TOKEN_SCOPE, false,
                    future -> {
                        try {
                            String auth = future.getResult().getString(KEY_AUTHTOKEN);
                            Request<?> request = createRequest(packageName, auth,
                                versionCode, queryData, onRequestFinished, onRequestError);
                            request.setShouldCache(false);
                            queue.add(request);
                        } catch (AuthenticatorException | IOException | OperationCanceledException e) {
@@ -138,28 +144,6 @@ public abstract class LicenseChecker<D, R> {
        }
    }

    public abstract Request<?> createRequest(String packageName, String auth, int versionCode, D data, BiConsumer<Integer, R> then, Response.ErrorListener errorListener);

    // Functional interfaces

    interface BiConsumerWithException<A, B, T extends Exception> {
        void accept(A a, B b) throws T;
    }

    interface BiConsumer<A, B> {
        void accept(A a, B b);
    }

    static class Tuple<A, B> {
        public final A a;
        public final B b;

        public Tuple(A a, B b) {
            this.a = a;
            this.b = b;
        }
    }

    // Implementations

    public static class V1 extends LicenseChecker<Long, Tuple<String, String>> {
@@ -199,4 +183,24 @@ public abstract class LicenseChecker<D, R> {
            );
        }
    }

    // Functional interfaces

    interface BiConsumerWithException<A, B, T extends Exception> {
        void accept(A a, B b) throws T;
    }

    interface BiConsumer<A, B> {
        void accept(A a, B b);
    }

    static class Tuple<A, B> {
        public final A a;
        public final B b;

        public Tuple(A a, B b) {
            this.a = a;
            this.b = b;
        }
    }
}