22
33import android .content .SharedPreferences ;
44import android .util .Log ;
5- import android .webkit .CookieManager ;
65import android .widget .Toast ;
76
87import androidx .annotation .NonNull ;
1413import com .fox2code .mmm .repo .RepoManager ;
1514import com .fox2code .mmm .repo .RepoModule ;
1615import com .fox2code .mmm .utils .Http ;
16+ import com .fox2code .mmm .utils .HttpException ;
1717import com .fox2code .mmm .utils .PropUtils ;
18+ import com .topjohnwu .superuser .internal .UiThreadHandler ;
1819
1920import org .json .JSONArray ;
2021import org .json .JSONException ;
2122import org .json .JSONObject ;
2223
2324import java .io .File ;
25+ import java .io .IOException ;
2426import java .nio .charset .StandardCharsets ;
2527import java .util .ArrayList ;
2628import java .util .Iterator ;
@@ -73,49 +75,58 @@ private static String filterURL(String url) {
7375 return url ;
7476 }
7577
76- public < string > boolean isValidToken (string token ) {
78+ public boolean isValidToken (String token ) throws IOException {
7779 try {
7880 Http .doHttpGet ("https://" + this .host + "/auth/me?token=" + token , false );
79- } catch (Exception e ) {
80- if ("Received error code: 419" .equals (e .getMessage ()) || "Received error code: 429" .equals (e .getMessage ())) {
81- Log .e (TAG , "We are being rate limited!" , e );
82- long time = System .currentTimeMillis ();
83- this .androidacyBlockade = time + 3_600_000L ;
81+ } catch (HttpException e ) {
82+ if (e .getErrorCode () == 401 ) {
83+ Log .w (TAG , "Invalid token, resetting..." );
84+ // Remove saved preference
85+ SharedPreferences .Editor editor = this .cachedPreferences .edit ();
86+ editor .remove ("androidacy_api_token" );
87+ editor .apply ();
8488 return false ;
8589 }
86- Log .w (TAG , "Invalid token, resetting..." );
87- // Remove saved preference
88- SharedPreferences .Editor editor = this .cachedPreferences .edit ();
89- editor .remove ("androidacy_api_token" );
90- editor .apply ();
91- return false ;
90+ throw e ;
9291 }
9392 // If status code is 200, we are good
9493 return true ;
9594 }
9695
9796 @ Override
9897 protected boolean prepare () {
98+ if (Http .needCaptchaAndroidacy ()) return false ;
9999 // Implementation details discussed on telegram
100100 // First, ping the server to check if it's alive
101101 try {
102102 Http .doHttpGet ("https://" + this .host + "/ping" , false );
103103 } catch (Exception e ) {
104104 Log .e (TAG , "Failed to ping server" , e );
105105 // Inform user
106- new Thread (() -> Toast .makeText (MainApplication .getINSTANCE (), R .string .androidacy_server_down , Toast .LENGTH_LONG ).show ()).start ();
106+ if (!HttpException .shouldTimeout (e )) {
107+ UiThreadHandler .run (() -> Toast .makeText (MainApplication .getINSTANCE (),
108+ R .string .androidacy_server_down , Toast .LENGTH_SHORT ).show ());
109+ }
107110 return false ;
108111 }
109112 long time = System .currentTimeMillis ();
110113 if (this .androidacyBlockade > time ) return false ;
111114 this .androidacyBlockade = time + 30_000L ;
112- if (this .token == null ) {
113- this .token = this .cachedPreferences .getString ("pref_androidacy_api_token" , null );
114- if (this .token != null && !this .isValidToken (this .token )) {
115+ try {
116+ if (this .token == null ) {
117+ this .token = this .cachedPreferences .getString ("pref_androidacy_api_token" , null );
118+ if (this .token != null && !this .isValidToken (this .token )) {
119+ this .token = null ;
120+ }
121+ } else if (!this .isValidToken (this .token )) {
115122 this .token = null ;
116123 }
117- } else if (!this .isValidToken (this .token )) {
118- this .token = null ;
124+ } catch (IOException e ) {
125+ if (HttpException .shouldTimeout (e )) {
126+ Log .e (TAG , "We are being rate limited!" , e );
127+ this .androidacyBlockade = time + 3_600_000L ;
128+ }
129+ return false ;
119130 }
120131 if (token == null ) {
121132 try {
@@ -142,7 +153,7 @@ protected boolean prepare() {
142153 // Save token to shared preference
143154 MainApplication .getSharedPreferences ().edit ().putString ("pref_androidacy_api_token" , token ).apply ();
144155 } catch (Exception e ) {
145- if ("Received error code: 419" . equals ( e . getMessage ()) || "Received error code: 429" . equals ( e . getMessage ()) || "Received error code: 503" . equals ( e . getMessage () )) {
156+ if (HttpException . shouldTimeout ( e )) {
146157 Log .e (TAG , "We are being rate limited!" , e );
147158 this .androidacyBlockade = time + 3_600_000L ;
148159 }
@@ -308,10 +319,8 @@ String getToken() {
308319 return this .token ;
309320 }
310321
311- void setToken (String token ) {
322+ public void setToken (String token ) {
312323 if (Http .hasWebView ()) {
313- // TODO: Figure out why this is needed
314- CookieManager .getInstance ().setCookie ("https://.androidacy.com/" , "USER=" + token + "; expires=Fri, 31 Dec 9999 23:59:59 GMT;" + " path=/; secure; domain=.androidacy.com" );
315324 this .token = token ;
316325 }
317326 }
0 commit comments