|
| 1 | +package com.noti.main.ui.prefs; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.util.Log; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.ProgressBar; |
| 8 | +import android.widget.TextView; |
| 9 | + |
| 10 | +import androidx.annotation.Nullable; |
| 11 | +import androidx.appcompat.app.AppCompatActivity; |
| 12 | +import androidx.appcompat.content.res.AppCompatResources; |
| 13 | +import androidx.appcompat.widget.AppCompatImageView; |
| 14 | + |
| 15 | +import com.android.volley.Response; |
| 16 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 17 | +import com.google.android.material.appbar.MaterialToolbar; |
| 18 | +import com.google.firebase.firestore.FirebaseFirestore; |
| 19 | +import com.google.firebase.firestore.QuerySnapshot; |
| 20 | + |
| 21 | +import com.noti.main.Application; |
| 22 | +import com.noti.main.R; |
| 23 | +import com.noti.main.service.backend.PacketConst; |
| 24 | +import com.noti.main.service.backend.PacketRequester; |
| 25 | +import com.noti.main.service.backend.ResultPacket; |
| 26 | + |
| 27 | +import org.json.JSONException; |
| 28 | +import org.json.JSONObject; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Locale; |
| 32 | +import java.util.Objects; |
| 33 | + |
| 34 | +@SuppressLint("SetTextI18n") |
| 35 | +public class ServerPingActivity extends AppCompatActivity { |
| 36 | + |
| 37 | + AppCompatImageView pingStateEmoji; |
| 38 | + ProgressBar pingStateProgress; |
| 39 | + TextView pingStateTitle; |
| 40 | + TextView pingStateDescription; |
| 41 | + |
| 42 | + long startTime; |
| 43 | + Response.Listener<JSONObject> successListener = response -> { |
| 44 | + try { |
| 45 | + pingStateProgress.setVisibility(View.GONE); |
| 46 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_accessibility_checkmark_24_regular)); |
| 47 | + pingStateTitle.setText("Server Calibration and Test Done."); |
| 48 | + String cause = """ |
| 49 | + Time taken: %d (ms) |
| 50 | + Server Version: %s |
| 51 | + """; |
| 52 | + setPingStateDescription(String.format(Locale.getDefault(), cause, (System.currentTimeMillis() - startTime), ResultPacket.parseFrom(response.toString()).getExtraData())); |
| 53 | + } catch (IOException e) { |
| 54 | + setError(e); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 60 | + super.onCreate(savedInstanceState); |
| 61 | + setContentView(R.layout.activity_ping_server); |
| 62 | + getWindow().setStatusBarColor(getResources().getColor(R.color.ui_bg_toolbar, null)); |
| 63 | + ((MaterialToolbar)findViewById(R.id.toolbar)).setNavigationOnClickListener((v) -> this.finish()); |
| 64 | + |
| 65 | + pingStateEmoji = findViewById(R.id.pingStateEmoji); |
| 66 | + pingStateProgress = findViewById(R.id.pingStateProgress); |
| 67 | + pingStateTitle = findViewById(R.id.pingStateTitle); |
| 68 | + pingStateDescription = findViewById(R.id.pingStateDescription); |
| 69 | + pingStateDescription.setVisibility(View.GONE); |
| 70 | + |
| 71 | + if (getCurrentDns().equals(PacketConst.API_DOMAIN)) { |
| 72 | + performDefaultDnsTest(); |
| 73 | + } else { |
| 74 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_plug_connected_24_regular)); |
| 75 | + pingStateTitle.setText("Performing current DNS ping test..."); |
| 76 | + performSavedDnsPing(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + void performSavedDnsPing() { |
| 81 | + startTime = System.currentTimeMillis(); |
| 82 | + try { |
| 83 | + JSONObject serverBody = new JSONObject(); |
| 84 | + PacketRequester.addToRequestQueue(this, PacketConst.SERVICE_TYPE_PING_SERVER, serverBody, successListener, error -> { |
| 85 | + if (error.networkResponse == null) { |
| 86 | + setError(error); |
| 87 | + } else { |
| 88 | + performDefaultDnsTest(); |
| 89 | + } |
| 90 | + }); |
| 91 | + } catch (JSONException e) { |
| 92 | + setError(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + void performDefaultDnsTest() { |
| 97 | + startTime = System.currentTimeMillis(); |
| 98 | + setDnsToPreference(PacketConst.API_DOMAIN); |
| 99 | + |
| 100 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_plug_connected_settings_24_regular)); |
| 101 | + pingStateTitle.setText("Performing built-in DNS ping test..."); |
| 102 | + |
| 103 | + try { |
| 104 | + JSONObject serverBody = new JSONObject(); |
| 105 | + PacketRequester.addToRequestQueue(this, PacketConst.SERVICE_TYPE_PING_SERVER, serverBody, successListener, error -> { |
| 106 | + if (error.networkResponse == null) { |
| 107 | + setError(error); |
| 108 | + } else { |
| 109 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_database_search_24_regular)); |
| 110 | + pingStateTitle.setText("Getting preferred DNS from Database..."); |
| 111 | + Log.d("ErrorCode", "Dns: " + getCurrentDns() + " Code: " + error.networkResponse); |
| 112 | + |
| 113 | + OnCompleteListener<QuerySnapshot> onDBCompleteListener = task -> { |
| 114 | + if (task.isSuccessful() && task.getResult() != null) { |
| 115 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_plug_connected_24_regular)); |
| 116 | + pingStateTitle.setText("Performing external DNS ping test..."); |
| 117 | + |
| 118 | + QuerySnapshot document = task.getResult(); |
| 119 | + String recommendedDns = document.getDocuments().get(0).getString("availableDns"); |
| 120 | + performExternalDnsPing(recommendedDns); |
| 121 | + } else { |
| 122 | + setError(Objects.requireNonNullElse(task.getException(), new IllegalStateException("Error occurred while trying to query DB"))); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + FirebaseFirestore mFirebaseFireStore = FirebaseFirestore.getInstance(); |
| 127 | + mFirebaseFireStore.collection("ApiKey") |
| 128 | + .get() |
| 129 | + .addOnCompleteListener(onDBCompleteListener); |
| 130 | + } |
| 131 | + }); |
| 132 | + } catch (Exception e) { |
| 133 | + setError(e); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + void performExternalDnsPing(String dnsAddress) { |
| 138 | + setDnsToPreference(dnsAddress); |
| 139 | + startTime = System.currentTimeMillis(); |
| 140 | + |
| 141 | + try { |
| 142 | + JSONObject serverBody = new JSONObject(); |
| 143 | + PacketRequester.addToRequestQueue(this, PacketConst.SERVICE_TYPE_PING_SERVER, serverBody, successListener, error -> { |
| 144 | + setError(error); |
| 145 | + if (error.networkResponse != null) { |
| 146 | + String cause = """ |
| 147 | + Time taken: %d (ms) |
| 148 | + Error code: %s |
| 149 | + """; |
| 150 | + setPingStateDescription(String.format(Locale.getDefault(), cause, (System.currentTimeMillis() - startTime), error.networkResponse.statusCode)); |
| 151 | + setDnsToPreference(PacketConst.API_DOMAIN); |
| 152 | + } |
| 153 | + } |
| 154 | + ); |
| 155 | + } catch (JSONException e) { |
| 156 | + setError(e); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + String getCurrentDns() { |
| 161 | + return getSharedPreferences(Application.PREFS_NAME, MODE_PRIVATE) |
| 162 | + .getString(PacketConst.API_PREFS_DOMAIN_KEY, PacketConst.API_DOMAIN); |
| 163 | + } |
| 164 | + |
| 165 | + void setDnsToPreference(String dns) { |
| 166 | + getSharedPreferences(Application.PREFS_NAME, MODE_PRIVATE).edit() |
| 167 | + .putString(PacketConst.API_PREFS_DOMAIN_KEY, dns).apply(); |
| 168 | + } |
| 169 | + |
| 170 | + void setError(Exception error) { |
| 171 | + pingStateProgress.setVisibility(View.GONE); |
| 172 | + pingStateEmoji.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_fluent_globe_error_24_regular)); |
| 173 | + pingStateTitle.setText("Server calibration failed."); |
| 174 | + |
| 175 | + String cause = """ |
| 176 | + Time taken: %d (ms) |
| 177 | + Exception: %s |
| 178 | + """; |
| 179 | + String message = Objects.requireNonNullElse(error.getMessage(), "No message"); |
| 180 | + setPingStateDescription(String.format(Locale.getDefault(), cause, (System.currentTimeMillis() - startTime), message)); |
| 181 | + } |
| 182 | + |
| 183 | + void setPingStateDescription(String description) { |
| 184 | + pingStateDescription.setVisibility(View.VISIBLE); |
| 185 | + pingStateDescription.setText(description); |
| 186 | + } |
| 187 | +} |
0 commit comments