导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - ShareRest.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 ShareRest.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.eveningoutpost.dexdrip.sharemodels;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.sharemodels.models.ExistingFollower;
import com.eveningoutpost.dexdrip.sharemodels.models.InvitationPayload;
import com.eveningoutpost.dexdrip.sharemodels.models.ShareAuthenticationBody;
import com.eveningoutpost.dexdrip.sharemodels.models.ShareUploadPayload;
import com.eveningoutpost.dexdrip.xdrip;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okio.Buffer;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;

public class ShareRest {
    public static String TAG = "ShareRest";
    private DexcomShare dexcomShareApi;
    private String password;
    private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String str) {
            if ("dexcom_account_name".equals(str)) {
                ShareRest.this.username = sharedPreferences.getString(str, null);
            } else if ("dexcom_account_password".equals(str)) {
                ShareRest.this.password = sharedPreferences.getString(str, null);
            } else if ("share_key".equals(str)) {
                ShareRest.this.serialNumber = sharedPreferences.getString(str, null);
            }
        }
    };
    private String serialNumber;
    private String sessionId;
    private SharedPreferences sharedPreferences;
    private String username;

    public ShareRest(Context context, OkHttpClient okHttpClient) {
        if (okHttpClient == null) {
            try {
                okHttpClient = getOkHttpClient();
            } catch (IllegalStateException e) {
                Log.wtf(TAG, "Illegal state exception: " + e);
                return;
            }
        }
        okHttpClient = okHttpClient == null ? getOkHttpClient() : okHttpClient;
        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.dexcomShareApi = (DexcomShare) new Retrofit.Builder().baseUrl(this.sharedPreferences.getBoolean("dex_share_us_acct", true) ? "https://share2.dexcom.com/ShareWebServices/Services/" : "https://shareous1.dexcom.com/ShareWebServices/Services/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create())).build().create(DexcomShare.class);
        this.sessionId = this.sharedPreferences.getString("dexcom_share_session_id", null);
        this.username = this.sharedPreferences.getString("dexcom_account_name", null);
        this.password = this.sharedPreferences.getString("dexcom_account_password", null);
        this.serialNumber = this.sharedPreferences.getString("share_key", null);
        if (this.sharedPreferences.getBoolean("engineering_mode", false)) {
            String trim = this.sharedPreferences.getString("share_test_key", "").trim();
            if (trim.length() > 4) {
                this.serialNumber = trim;
            }
        }
        this.sharedPreferences.registerOnSharedPreferenceChangeListener(this.preferenceChangeListener);
        if ("".equals(this.sessionId)) {
            this.sessionId = null;
        }
    }

    private synchronized OkHttpClient getOkHttpClient() {
        OkHttpClient okHttpClient;
        try {
            TrustManager[] trustManagerArr = {new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }};
            SSLContext sSLContext = SSLContext.getInstance("SSL");
            sSLContext.init(null, trustManagerArr, new SecureRandom());
            SSLSocketFactory socketFactory = sSLContext.getSocketFactory();
            okHttpClient = new OkHttpClient();
            okHttpClient.networkInterceptors().add(new Interceptor() {
                public synchronized Response intercept(Interceptor.Chain chain) throws IOException {
                    try {
                        Request request = chain.request();
                        Request build = request.newBuilder().build();
                        Request build2 = request.newBuilder().header("User-Agent", "CGM-Store-1.2/22 CFNetwork/711.5.6 Darwin/14.0.0").header("Content-Type", "application/json").header("Accept", "application/json").build();
                        Log.d(ShareRest.TAG, "Sending request: " + build2.toString());
                        Buffer buffer = new Buffer();
                        build.body().writeTo(buffer);
                        Log.d(ShareRest.TAG, "Request body: " + buffer.readUtf8());
                        Response proceed = chain.proceed(build2);
                        Log.d(ShareRest.TAG, "Received response: " + proceed.toString());
                        if (proceed.body() == null) {
                            return proceed;
                        }
                        MediaType contentType = proceed.body().contentType();
                        String string = proceed.body().string();
                        Log.d(ShareRest.TAG, "Response body: " + string);
                        return proceed.newBuilder().body(ResponseBody.create(contentType, string)).build();
                    } catch (IllegalStateException e) {
                        UserError.Log.wtf(ShareRest.TAG, "Got illegal state exception: " + e);
                        return null;
                    } catch (NullPointerException e2) {
                        Log.e(ShareRest.TAG, "Got null pointer exception: " + e2);
                        return null;
                    }
                }
            });
            okHttpClient.setSslSocketFactory(socketFactory);
            okHttpClient.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String str, SSLSession sSLSession) {
                    return true;
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("Error occurred initializing OkHttp: ", e);
        }
        return okHttpClient;
    }

    public String getSessionId() {
        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
            @Override
            public String doInBackground(String... strArr) {
                try {
                    Boolean bool = strArr[0] != null ? (Boolean) ShareRest.this.dexcomShareApi.checkSessionActive(strArr[0]).execute().body() : null;
                    if (bool != null && bool.booleanValue()) {
                        return strArr[0];
                    }
                    return updateAuthenticationParams();
                } catch (IOException unused) {
                    return null;
                } catch (RuntimeException e) {
                    UserError.Log.wtf(ShareRest.TAG, "Painful exception processing response in updateAuthenticationParams " + e);
                    return null;
                }
            }

            private String updateAuthenticationParams() throws IOException {
                ShareRest shareRest = ShareRest.this;
                shareRest.sessionId = (String) shareRest.dexcomShareApi.getSessionId(new ShareAuthenticationBody(ShareRest.this.password, ShareRest.this.username).toMap()).execute().body();
                ShareRest.this.dexcomShareApi.authenticatePublisherAccount(ShareRest.this.sessionId, ShareRest.this.serialNumber, new ShareAuthenticationBody(ShareRest.this.password, ShareRest.this.username).toMap()).execute().body();
                ShareRest.this.dexcomShareApi.StartRemoteMonitoringSession(ShareRest.this.sessionId, ShareRest.this.serialNumber).execute();
                String str = (String) ShareRest.this.dexcomShareApi.checkMonitorAssignment(ShareRest.this.sessionId, ShareRest.this.serialNumber).execute().body();
                if (str != null && !str.equals("AssignedToYou")) {
                    ShareRest.this.dexcomShareApi.updateMonitorAssignment(ShareRest.this.sessionId, ShareRest.this.serialNumber).execute();
                }
                return ShareRest.this.sessionId;
            }
        };
        String str = this.sessionId;
        if (str == null || str.equals("")) {
            try {
                this.sessionId = asyncTask.executeOnExecutor(xdrip.executor, this.sessionId).get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        return this.sessionId;
    }

    public void getContacts(Callback<List<ExistingFollower>> callback) {
        this.dexcomShareApi.getContacts(getSessionId()).enqueue(new AuthenticatingCallback<List<ExistingFollower>>(callback) {
            @Override
            public void onRetry() {
                ShareRest.this.dexcomShareApi.getContacts(ShareRest.this.getSessionId()).enqueue(this);
            }
        });
    }

    public void uploadBGRecords(final ShareUploadPayload shareUploadPayload, Callback<ResponseBody> callback) {
        this.dexcomShareApi.uploadBGRecords(getSessionId(), shareUploadPayload).enqueue(new AuthenticatingCallback<ResponseBody>(callback) {
            @Override
            public void onRetry() {
                ShareRest.this.dexcomShareApi.uploadBGRecords(ShareRest.this.getSessionId(), shareUploadPayload).enqueue(this);
            }
        });
    }

    public void createContact(final String str, final String str2, Callback<String> callback) {
        this.dexcomShareApi.createContact(getSessionId(), str, str2).enqueue(new AuthenticatingCallback<String>(callback) {
            @Override
            public void onRetry() {
                ShareRest.this.dexcomShareApi.createContact(ShareRest.this.getSessionId(), str, str2).enqueue(this);
            }
        });
    }

    public void createInvitationForContact(final String str, final InvitationPayload invitationPayload, Callback<String> callback) {
        this.dexcomShareApi.createInvitationForContact(getSessionId(), str, invitationPayload).enqueue(new AuthenticatingCallback<String>(callback) {
            @Override
            public void onRetry() {
                ShareRest.this.dexcomShareApi.createInvitationForContact(ShareRest.this.getSessionId(), str, invitationPayload).enqueue(this);
            }
        });
    }

    public void deleteContact(final String str, Callback<ResponseBody> callback) {
        this.dexcomShareApi.deleteContact(getSessionId(), str).enqueue(new AuthenticatingCallback<ResponseBody>(callback) {
            @Override
            public void onRetry() {
                ShareRest.this.dexcomShareApi.deleteContact(ShareRest.this.getSessionId(), str).enqueue(this);
            }
        });
    }

    public abstract class AuthenticatingCallback<T> implements Callback<T> {
        private int attempts = 0;
        private Callback<T> delegate;

        public abstract void onRetry();

        public AuthenticatingCallback(Callback<T> callback) {
            this.delegate = callback;
        }

        public void onResponse(retrofit.Response<T> response, Retrofit retrofit) {
            int i;
            if (response.code() == 500 && (i = this.attempts) == 0) {
                this.attempts = i + 1;
                ShareRest.this.dexcomShareApi.getSessionId(new ShareAuthenticationBody(ShareRest.this.password, ShareRest.this.username).toMap()).enqueue(new Callback<String>() {
                    public void onResponse(retrofit.Response<String> response2, Retrofit retrofit2) {
                        if (response2.isSuccess()) {
                            ShareRest.this.sessionId = (String) response2.body();
                            ShareRest.this.sharedPreferences.edit().putString("dexcom_share_session_id", ShareRest.this.sessionId).apply();
                            AuthenticatingCallback.this.onRetry();
                        }
                    }

                    public void onFailure(Throwable th) {
                        AuthenticatingCallback.this.delegate.onFailure(th);
                    }
                });
            } else {
                this.delegate.onResponse(response, retrofit);
            }
        }

        public void onFailure(Throwable th) {
            this.delegate.onFailure(th);
        }
    }
}