aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/ext/IService.java
blob: ea0b136990f91390ae57374ed62e9c2f7680dd5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package moe.yuuta.ext;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;

import androidx.annotation.NonNull;

import moe.yuuta.gplicense.util.Base64;
import moe.yuuta.gplicense.util.Base64DecoderException;

public interface IService extends IInterface {
    abstract class Stub extends Binder implements IService {
        private static final String DESCRIPTOR;

        static {
            try {
                // Base64 encoded -
                // com.android.vending.licensing.ILicensingService
                DESCRIPTOR = new String(Base64.decode(
                        "Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U="));
            } catch (Base64DecoderException e) {
                throw new RuntimeException(e);
            }
        }

        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        public static IService asInterface(IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (iin instanceof IService) {
                return (IService) iin;
            }
            return new IService.Stub.Proxy(obj);
        }

        @Override
        public IBinder asBinder() {
            return this;
        }

        @Override
        public boolean onTransact(int code, @NonNull Parcel data, Parcel reply, int flags) throws RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_checkLicense: {
                    data.enforceInterface(DESCRIPTOR);
                    long nonce = data.readLong();
                    String packageName = data.readString();
                    IResultListener listener = IResultListener.Stub.asInterface(data.readStrongBinder());
                    this.exec(nonce, packageName, listener);
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }
        }

        private static class Proxy implements IService {
            private IBinder mRemote;

            Proxy(IBinder remote) {
                mRemote = remote;
            }

            @Override
            public IBinder asBinder() {
                return mRemote;
            }
            
            @Override
            public void exec(long nonce, String packageName, IResultListener listener) throws RemoteException {
                Parcel args = Parcel.obtain();
                try {
                    args.writeInterfaceToken(DESCRIPTOR);
                    args.writeLong(nonce);
                    args.writeString(packageName);
                    args.writeStrongBinder(listener != null ? listener.asBinder() : null);
                    mRemote.transact(TRANSACTION_checkLicense, args, null, IBinder.FLAG_ONEWAY);
                } finally {
                    args.recycle();
                }
            }
        }

        static final int TRANSACTION_checkLicense = IBinder.FIRST_CALL_TRANSACTION;
    }

    // checkLicense
    void exec(long nonce, String packageName, IResultListener listener) throws RemoteException;
}