1+ package com .doxart .ivpn .Activities ;
2+
3+ import androidx .appcompat .app .AppCompatActivity ;
4+ import androidx .core .view .WindowInsetsCompat ;
5+ import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
6+
7+ import android .content .BroadcastReceiver ;
8+ import android .content .Context ;
9+ import android .content .Intent ;
10+ import android .content .IntentFilter ;
11+ import android .os .Bundle ;
12+ import android .util .TypedValue ;
13+ import android .view .WindowManager ;
14+
15+ import com .bumptech .glide .Glide ;
16+ import com .doxart .ivpn .Model .ServerModel ;
17+ import com .doxart .ivpn .R ;
18+ import com .doxart .ivpn .Util .Utils ;
19+ import com .doxart .ivpn .databinding .ActivityConnectionReportBinding ;
20+ import com .google .android .gms .ads .AdRequest ;
21+
22+ public class ConnectionReportActivity extends AppCompatActivity {
23+
24+ ActivityConnectionReportBinding b ;
25+ public static ServerModel server ;
26+ private boolean isConnection ;
27+
28+ @ Override
29+ protected void onCreate (Bundle savedInstanceState ) {
30+ super .onCreate (savedInstanceState );
31+ inflate ();
32+ }
33+
34+ private void inflate () {
35+ b = ActivityConnectionReportBinding .inflate (getLayoutInflater ());
36+ setContentView (b .getRoot ());
37+
38+ getWindow ().setFlags (WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS , WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS );
39+
40+ adjustMargins ();
41+ setupView ();
42+ init ();
43+ }
44+
45+ private void adjustMargins () {
46+ int statusBarHeight = MainActivity .getInstance ().getInsetsCompat ().getInsets (WindowInsetsCompat .Type .statusBars ()).top ;
47+ int navigationBarHeight = MainActivity .getInstance ().getInsetsCompat ().getInsets (WindowInsetsCompat .Type .navigationBars ()).bottom ;
48+
49+ int pxToDp = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 20 , getResources ().getDisplayMetrics ());
50+
51+ b .appbar .setPadding (0 , statusBarHeight , 0 , 0 );
52+ }
53+
54+ private void setupView () {
55+ if (server != null ) {
56+ isConnection = getIntent ().getBooleanExtra ("isConnection" , true );
57+
58+ if (isConnection ) b .connectionTypeTxt .setText (getString (R .string .connection_successful ));
59+ else {
60+ b .connectionTypeTxt .setText (getString (R .string .connection_report ));
61+ updateConnectionStatus (getIntent ().getIntExtra ("sessionM" , 0 ), getIntent ().getIntExtra ("sessionS" , 0 ));
62+ }
63+
64+ Glide .with (this ).load ("https://flagcdn.com/h80/" + server .getFlagUrl () + ".png" ).centerCrop ().into (b .serverFlag );
65+ b .serverIp .setText (server .getIpv4 ());
66+ b .serverName .setText (server .getCountry ());
67+ } else finish ();
68+ }
69+
70+ private void init () {
71+ b .adView .loadAd (new AdRequest .Builder ().build ());
72+ b .closeBT .setOnClickListener (v -> finish ());
73+
74+ b .shareBT .setOnClickListener (v -> Utils .shareApp (this ));
75+ b .ratingView .setOnClickListener (v -> Utils .openAppInPlayStore (this ));
76+ }
77+
78+ BroadcastReceiver broadcastReceiver = new BroadcastReceiver () {
79+ @ Override
80+ public void onReceive (Context context , Intent intent ) {
81+ if (intent .getAction ().equals ("usage_data_updated" )) {
82+ int m = intent .getIntExtra ("usageMinutes" , 0 );
83+ int s = intent .getIntExtra ("usageSeconds" , 0 );
84+ updateConnectionStatus (m , s );
85+ }
86+ }
87+ };
88+
89+ public void updateConnectionStatus (int m , int s ) {
90+ int totalSeconds = m * 60 + s ;
91+ String formattedTime = String .format ("%02d s" , totalSeconds % 60 );
92+ b .serverTime .setText (formattedTime );
93+ }
94+
95+ @ Override
96+ public void onResume () {
97+ LocalBroadcastManager .getInstance (this ).registerReceiver (broadcastReceiver , new IntentFilter ("connectionState" ));
98+ LocalBroadcastManager .getInstance (this ).registerReceiver (broadcastReceiver , new IntentFilter ("usage_data_updated" ));
99+ super .onResume ();
100+ }
101+
102+ @ Override
103+ public void onPause () {
104+ LocalBroadcastManager .getInstance (this ).unregisterReceiver (broadcastReceiver );
105+ super .onPause ();
106+ }
107+ }
0 commit comments