|
3 | 3 | import android.app.Application;
|
4 | 4 | import android.util.Log;
|
5 | 5 |
|
| 6 | +import androidx.annotation.NonNull; |
| 7 | + |
| 8 | +import com.squareup.moshi.Moshi; |
| 9 | +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory; |
| 10 | + |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.walletconnect.Session; |
| 13 | +import org.walletconnect.impls.FileWCSessionStore; |
| 14 | +import org.walletconnect.impls.MoshiPayloadAdapter; |
| 15 | +import org.walletconnect.impls.OkHttpTransport; |
| 16 | +import org.walletconnect.impls.WCSession; |
| 17 | +import org.walletconnect.impls.WCSessionStore; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Random; |
| 23 | +import java.util.UUID; |
| 24 | + |
| 25 | +import ai.elimu.crowdsource.server.BridgeServer; |
6 | 26 | import ai.elimu.crowdsource.util.SharedPreferencesHelper;
|
7 | 27 | import ai.elimu.crowdsource.util.VersionHelper;
|
8 | 28 | import ai.elimu.model.v2.enums.Language;
|
| 29 | +import kotlin.Unit; |
| 30 | +import kotlin.jvm.functions.Function1; |
| 31 | +import kotlin.jvm.internal.Intrinsics; |
| 32 | +import okhttp3.OkHttpClient; |
9 | 33 | import retrofit2.Retrofit;
|
10 | 34 | import retrofit2.converter.gson.GsonConverterFactory;
|
11 | 35 | import timber.log.Timber;
|
12 | 36 |
|
13 | 37 | public class BaseApplication extends Application {
|
| 38 | + private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
| 39 | + public static Session.FullyQualifiedConfig config; |
| 40 | + public static Session session; |
| 41 | + private static OkHttpClient client; |
| 42 | + private static Moshi moshi; |
| 43 | + private static BridgeServer bridge; |
| 44 | + private static WCSessionStore storage; |
14 | 45 |
|
15 | 46 | @Override
|
16 | 47 | public void onCreate() {
|
17 | 48 | Log.i(getClass().getName(), "onCreate");
|
18 | 49 | super.onCreate();
|
19 |
| - |
20 |
| - // Log config |
21 |
| - Timber.plant(new Timber.DebugTree()); |
| 50 | + this.initMoshi(); |
| 51 | + this.initClient(); |
| 52 | + this.initBridge(); |
| 53 | + this.initSessionStorage(); |
| 54 | + if(BuildConfig.DEBUG){ |
| 55 | + Timber.plant(new Timber.DebugTree()); |
| 56 | + } |
22 | 57 | Timber.i("onCreate");
|
23 | 58 |
|
24 | 59 | VersionHelper.updateAppVersion(getApplicationContext());
|
@@ -48,4 +83,111 @@ public String getBaseUrl() {
|
48 | 83 | public String getRestUrl() {
|
49 | 84 | return getBaseUrl() + "/rest/v2";
|
50 | 85 | }
|
| 86 | + |
| 87 | + |
| 88 | + private void initClient() { |
| 89 | + OkHttpClient var10000 = (new OkHttpClient.Builder()).build(); |
| 90 | + Intrinsics.checkNotNullExpressionValue(var10000, "OkHttpClient.Builder().build()"); |
| 91 | + client = var10000; |
| 92 | + } |
| 93 | + |
| 94 | + private void initMoshi() { |
| 95 | + Moshi var10000 = (new com.squareup.moshi.Moshi.Builder()).add(new KotlinJsonAdapterFactory()).build(); |
| 96 | + Intrinsics.checkNotNullExpressionValue(var10000, "Moshi.Builder().build()"); |
| 97 | + moshi = var10000; |
| 98 | + } |
| 99 | + |
| 100 | + private void initBridge() { |
| 101 | + bridge = new BridgeServer(moshi); |
| 102 | + bridge.start(); |
| 103 | + } |
| 104 | + |
| 105 | + private void initSessionStorage() { |
| 106 | + File tmp = new File(this.getCacheDir(), "session_store.json"); |
| 107 | + try { |
| 108 | + tmp.createNewFile(); |
| 109 | + storage = new FileWCSessionStore(tmp, moshi); |
| 110 | + } catch (IOException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + @NotNull |
| 118 | + public static String bytesToHex(byte[] bytes) { |
| 119 | + Intrinsics.checkNotNullParameter(bytes, "bytes"); |
| 120 | + char[] hexChars = new char[bytes.length * 2]; |
| 121 | + int j = 0; |
| 122 | + |
| 123 | + for (int var4 = bytes.length; j < var4; ++j) { |
| 124 | + int v = bytes[j] & 255; |
| 125 | + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
| 126 | + hexChars[j * 2 + 1] = HEX_ARRAY[v & 15]; |
| 127 | + } |
| 128 | + |
| 129 | + return new String(hexChars); |
| 130 | + } |
| 131 | + |
| 132 | + public static void resetSession() throws Exception { |
| 133 | + if (session != null) { |
| 134 | + session.clearCallbacks(); |
| 135 | + } |
| 136 | + byte[] randomBytes = new byte[32]; |
| 137 | + Random r = new Random(); |
| 138 | + r.nextBytes(randomBytes); |
| 139 | + String key = bytesToHex(randomBytes); |
| 140 | + String uuid = UUID.randomUUID().toString(); |
| 141 | + config = new Session.FullyQualifiedConfig(uuid, "http://localhost:" + BridgeServer.Companion.getPORT(), key, "wc", 1); |
| 142 | +// config = new Session.FullyQualifiedConfig(uuid, "https://bridge.walletconnect.org", "f70af2060965927b7e709503e71b3cbf", "wc", 1); |
| 143 | + session = new WCSession( |
| 144 | + config, |
| 145 | + new WrappedMoshiPayloadAdapter(new MoshiPayloadAdapter(moshi)), |
| 146 | + storage, |
| 147 | + new WrappedOkHttpTransportBuilder(new OkHttpTransport.Builder(client, moshi)), |
| 148 | + new Session.PeerMeta( |
| 149 | + "elimu.ai", |
| 150 | + "Elimu Crowdsource App", |
| 151 | + "Elimu Crowdsource App", |
| 152 | + Collections.emptyList() |
| 153 | + ), |
| 154 | + null, |
| 155 | + null |
| 156 | + ); |
| 157 | + session.offer(); |
| 158 | + } |
| 159 | + |
| 160 | + static class WrappedMoshiPayloadAdapter implements Session.PayloadAdapter { |
| 161 | + private final MoshiPayloadAdapter wrapped; |
| 162 | + |
| 163 | + public WrappedMoshiPayloadAdapter(MoshiPayloadAdapter wrapped) { |
| 164 | + this.wrapped = wrapped; |
| 165 | + } |
| 166 | + |
| 167 | + @NonNull |
| 168 | + @Override |
| 169 | + public Session.MethodCall parse(@NonNull String s, @NonNull String s1) { |
| 170 | + return this.wrapped.parse(s, s1); |
| 171 | + } |
| 172 | + |
| 173 | + @NonNull |
| 174 | + @Override |
| 175 | + public String prepare(@NonNull Session.MethodCall methodCall, @NonNull String s) { |
| 176 | + return this.wrapped.prepare(methodCall, s); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + static class WrappedOkHttpTransportBuilder implements Session.Transport.Builder { |
| 181 | + private final OkHttpTransport.Builder wrapped; |
| 182 | + |
| 183 | + public WrappedOkHttpTransportBuilder(OkHttpTransport.Builder wrapped) { |
| 184 | + this.wrapped = wrapped; |
| 185 | + } |
| 186 | + |
| 187 | + @NonNull |
| 188 | + @Override |
| 189 | + public Session.Transport build(@NonNull String s, @NonNull Function1<? super Session.Transport.Status, Unit> function1, @NonNull Function1<? super Session.Transport.Message, Unit> function11) { |
| 190 | + return this.wrapped.build(s, function1, function11); |
| 191 | + } |
| 192 | + } |
51 | 193 | }
|
0 commit comments