66import android .os .Handler ;
77import android .os .Looper ;
88import android .view .View ;
9+ import android .view .ViewGroup ;
10+
11+ import androidx .core .view .ViewCompat ;
12+ import androidx .core .view .WindowInsetsCompat ;
913
1014import com .google .android .material .snackbar .Snackbar ;
1115
@@ -34,13 +38,13 @@ public UpdateChecker(Context context) {
3438 this .client = new OkHttpClient .Builder ().retryOnConnectionFailure (true ).build ();
3539 }
3640
37- public void checkForUpdates (Runnable onComplete ) {
41+ public void checkForUpdates (View view , Runnable onComplete ) {
3842 CompletableFuture .supplyAsync (this ::fetchLatestVersion ).thenAccept (latestVersion -> runOnMainThread (() -> {
39- handleLatestVersion (latestVersion );
43+ handleLatestVersion (latestVersion , view );
4044 onComplete .run ();
4145 })).exceptionally (throwable -> {
4246 runOnMainThread (() -> {
43- handleError (throwable );
47+ handleError (throwable , view );
4448 onComplete .run ();
4549 });
4650 return null ;
@@ -49,11 +53,9 @@ public void checkForUpdates(Runnable onComplete) {
4953
5054 private String fetchLatestVersion () {
5155 Request request = new Request .Builder ().url (GITHUB_API_URL ).build ();
52-
5356 try (Response response = client .newCall (request ).execute ()) {
5457 if (response .isSuccessful () && response .body () != null ) {
55- String jsonData = response .body ().string ();
56- return parseLatestVersion (jsonData );
58+ return parseLatestVersion (response .body ().string ());
5759 } else {
5860 throw new IOException ("Unsuccessful response: " + response .code ());
5961 }
@@ -71,54 +73,54 @@ private String parseLatestVersion(String jsonData) {
7173 }
7274 }
7375
74- private void handleLatestVersion (String latestVersion ) {
76+ private void handleLatestVersion (String latestVersion , View view ) {
7577 Context context = contextRef .get ();
76- if (context == null ) return ;
77-
78- View rootView = getRootView (context );
79- if (rootView == null ) return ;
78+ if (context == null || view == null ) return ;
8079
8180 String appVersion = context .getString (R .string .github_tag_version );
8281
8382 if (appVersion .compareTo (latestVersion ) < 0 ) {
84- showSnackbarWithAction (rootView , context .getString (R .string .update_available ), context .getString (R .string .snackbar_button_download ), v -> openGitHubReleaseLink (context ));
83+ showSnackbarWithAction (view , context .getString (R .string .update_available ), context .getString (R .string .snackbar_button_download ), v -> openGitHubReleaseLink (context ));
8584 } else {
86- showSnackbar (rootView , context .getString (R .string .no_update_available ));
85+ showSnackbar (view , context .getString (R .string .no_update_available ));
8786 }
8887 }
8988
90- private void handleError (Throwable throwable ) {
89+ private void handleError (Throwable throwable , View view ) {
9190 Context context = contextRef .get ();
92- if (context == null ) return ;
91+ if (context == null || view == null ) return ;
9392
94- View rootView = getRootView ( context );
95- if ( rootView != null ) {
96- String errorMessage = throwable . getMessage () != null && throwable . getMessage (). toLowerCase (). contains ( "connection" ) ? context . getString ( R . string . error_connection ) + " " + throwable . getMessage () : context . getString ( R . string . error_others ) + " " + throwable . getMessage ();
93+ String errorMessage = throwable . getMessage () != null && throwable . getMessage (). toLowerCase (). contains ( "connection" ) ? context . getString ( R . string . error_connection ) + " " + throwable . getMessage () : context . getString ( R . string . error_others ) + " " + throwable . getMessage ( );
94+ showSnackbar ( view , errorMessage );
95+ }
9796
98- showSnackbar (rootView , errorMessage );
99- }
97+ private void showSnackbar (View view , String message ) {
98+ Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG );
99+ configureSnackbar (snackbar );
100+ snackbar .show ();
100101 }
101102
102- private void showSnackbar (View rootView , String message ) {
103- Snackbar .make (rootView , message , Snackbar .LENGTH_LONG ).show ();
103+ private void showSnackbarWithAction (View view , String message , String actionText , View .OnClickListener actionListener ) {
104+ Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG ).setAction (actionText , actionListener );
105+ configureSnackbar (snackbar );
106+ snackbar .show ();
104107 }
105108
106- private void showSnackbarWithAction (View rootView , String message , String actionText , View .OnClickListener actionListener ) {
107- Snackbar .make (rootView , message , Snackbar .LENGTH_LONG ).setAction (actionText , actionListener ).show ();
109+ private void configureSnackbar (Snackbar snackbar ) {
110+ ViewCompat .setOnApplyWindowInsetsListener (snackbar .getView (), (v , insets ) -> {
111+ int bottomPadding = insets .getInsets (WindowInsetsCompat .Type .systemBars ()).bottom ;
112+ ViewGroup .MarginLayoutParams params = (ViewGroup .MarginLayoutParams ) v .getLayoutParams ();
113+ params .bottomMargin = bottomPadding ;
114+ v .setLayoutParams (params );
115+ return insets ;
116+ });
108117 }
109118
110119 private void openGitHubReleaseLink (Context context ) {
111120 Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (GITHUB_RELEASE_LINK ));
112121 context .startActivity (intent );
113122 }
114123
115- private View getRootView (Context context ) {
116- if (context instanceof android .app .Activity ) {
117- return ((android .app .Activity ) context ).findViewById (android .R .id .content );
118- }
119- return null ;
120- }
121-
122124 private void runOnMainThread (Runnable action ) {
123125 new Handler (Looper .getMainLooper ()).post (action );
124126 }
0 commit comments