2
2
3
3
import android .accounts .Account ;
4
4
import android .accounts .AccountManager ;
5
+ import android .app .Activity ;
5
6
import android .content .Context ;
7
+ import android .content .Intent ;
6
8
import android .content .pm .ApplicationInfo ;
7
9
import android .content .pm .PackageManager ;
8
10
import android .os .Bundle ;
11
+ import android .os .Handler ;
9
12
import android .util .Log ;
10
13
11
14
import com .google .gson .JsonObject ;
17
20
import androidx .annotation .Nullable ;
18
21
import io .syslogic .github .BuildConfig ;
19
22
import io .syslogic .github .Constants ;
23
+ import io .syslogic .github .activity .AuthenticatorActivity ;
24
+ import io .syslogic .github .activity .NavHostActivity ;
20
25
import io .syslogic .github .api .GithubClient ;
21
26
import io .syslogic .github .api .model .User ;
22
- import okhttp3 .ResponseBody ;
27
+ import io .syslogic .github .fragment .HomeScreenFragment ;
28
+ import io .syslogic .github .fragment .HomeScreenFragmentDirections ;
29
+
23
30
import retrofit2 .Call ;
24
31
import retrofit2 .Callback ;
25
32
import retrofit2 .Response ;
@@ -38,21 +45,35 @@ public class TokenHelper {
38
45
static final boolean mDebug = BuildConfig .DEBUG ;
39
46
40
47
@ Nullable
41
- public static String getAccessToken (@ NonNull Context context ) {
42
- AccountManager accountManager = AccountManager .get (context );
48
+ public static String getAccessToken (@ NonNull Activity activity ) {
49
+ AccountManager accountManager = AccountManager .get (activity );
43
50
Account account = getAccount (accountManager , 0 );
44
51
if (account != null ) {
45
52
/* Default: Load the access token from AccountManager. */
46
53
return accountManager .getUserData (account , "token" );
47
54
} else if (mDebug ) {
48
55
/* Debug: Try to load and validate the access token. */
49
- return loadTokenFromPackageMeta (context , accountManager );
56
+ return loadTokenFromPackageMeta (activity , accountManager );
50
57
} else {
51
58
Log .e (LOG_TAG , "Account not found: " + Constants .ACCOUNT_TYPE );
52
59
return null ;
53
60
}
54
61
}
55
62
63
+ public static void setAccessToken (Activity activity , @ Nullable String token ) {
64
+ AccountManager accountManager = AccountManager .get (activity );
65
+ Account account = getAccount (accountManager , 0 );
66
+ if (account != null && token == null ) {
67
+ accountManager .removeAccount (account , activity , accountManagerFuture -> {
68
+ Log .d (LOG_TAG , "Account removed: " + Constants .ACCOUNT_TYPE );
69
+ }, new Handler ());
70
+ } else if (account == null && token == null ) {
71
+ /* This maybe happen when the token loaded from package-meta has expired. */
72
+ Intent intent = new Intent (activity , AuthenticatorActivity .class );
73
+ activity .startActivity (intent );
74
+ }
75
+ }
76
+
56
77
/** The username is now being stored along with the token */
57
78
@ Nullable
58
79
public static String getUsername (@ NonNull Context context ) {
@@ -67,20 +88,20 @@ public static String getUsername(@NonNull Context context) {
67
88
}
68
89
}
69
90
70
- private static String loadTokenFromPackageMeta (@ NonNull Context context , AccountManager accountManager ) {
91
+ private static String loadTokenFromPackageMeta (@ NonNull Activity activity , AccountManager accountManager ) {
71
92
String token = null ;
72
93
try {
73
94
74
95
ApplicationInfo app ;
75
96
if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .TIRAMISU ) {
76
97
PackageManager .ApplicationInfoFlags flags = PackageManager .ApplicationInfoFlags .of (PackageManager .GET_META_DATA );
77
- app = context .getPackageManager ().getApplicationInfo (context .getPackageName (), flags );
98
+ app = activity .getPackageManager ().getApplicationInfo (activity .getPackageName (), flags );
78
99
} else {
79
- app = context .getPackageManager ().getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
100
+ app = activity .getPackageManager ().getApplicationInfo (activity .getPackageName (), PackageManager .GET_META_DATA );
80
101
}
81
102
82
- if (app .metaData .keySet ().contains ("com.github.ACCESS_TOKEN" )) {
83
-
103
+ if (mDebug && app .metaData .keySet ().contains ("com.github.ACCESS_TOKEN" )) {
104
+ Log . d ( LOG_TAG , "loading access token from meat-data: " + Constants . ACCOUNT_TYPE );
84
105
token = app .metaData .getString ("com.github.ACCESS_TOKEN" );
85
106
if (token != null && !token .equals ("null" )) {
86
107
@@ -96,29 +117,46 @@ public void onResponse(@NonNull Call<User> call, @NonNull Response<User> respons
96
117
if (response .body () != null ) {
97
118
User item = response .body ();
98
119
Account account = addAccount (accountManager , item .getLogin (), finalToken );
99
- if (mDebug ) {
100
- if (account != null ) {Log .d (LOG_TAG , "account added" );}
101
- else {Log .d (LOG_TAG , "account not added" );}
102
- }
120
+ if (account != null ) {Log .d (LOG_TAG , "account added" );}
121
+ else {Log .d (LOG_TAG , "account not added" );}
103
122
}
104
- } else {
105
- /* "bad credentials" means that the provided access-token is invalid. */
106
- if (response .errorBody () != null ) {
107
- logError (response .errorBody ());
123
+ } else if (response .errorBody () != null ) {
124
+ try {
125
+ String errors = response .errorBody ().string ();
126
+ JsonObject jsonObject = JsonParser .parseString (errors ).getAsJsonObject ();
127
+ String message = jsonObject .get ("message" ).toString ();
128
+
129
+ /* "Bad credentials" means that the provided access-token is invalid. */
130
+ if (BuildConfig .DEBUG ) {Log .e (LOG_TAG , "login error: " + message );}
131
+ if (message .equals ("\" Bad credentials\" " )) {
132
+
133
+ // Remove the token, it is invalid anyway.
134
+ TokenHelper .setAccessToken (activity , null );
135
+
136
+ if (activity instanceof NavHostActivity activity2 ) {
137
+ if (activity2 .getCurrentFragment () instanceof HomeScreenFragment ) {
138
+ activity2 .getNavController ().navigate (
139
+ HomeScreenFragmentDirections
140
+ .actionHomeScreenFragmentToPreferencesFragment ()
141
+ );
142
+ }
143
+ }
144
+ }
145
+ } catch (IOException e ) {
146
+ if (BuildConfig .DEBUG ) {Log .e (LOG_TAG , "" + e .getMessage ());}
108
147
}
109
148
}
110
149
}
111
150
@ Override
112
151
public void onFailure (@ NonNull Call <User > call , @ NonNull Throwable t ) {
113
- if ( mDebug ) { Log .e (LOG_TAG , "" + t .getMessage ());}
152
+ Log .e (LOG_TAG , "" + t .getMessage ());
114
153
}
115
154
});
116
155
}
117
156
}
118
157
} catch (NullPointerException | PackageManager .NameNotFoundException e ) {
119
158
e .printStackTrace ();
120
159
}
121
-
122
160
return token ;
123
161
}
124
162
@@ -143,14 +181,4 @@ public static Account addAccount(AccountManager accountManager, String username,
143
181
}
144
182
return null ;
145
183
}
146
-
147
- static void logError (@ NonNull ResponseBody responseBody ) {
148
- try {
149
- String errors = responseBody .string ();
150
- JsonObject jsonObject = JsonParser .parseString (errors ).getAsJsonObject ();
151
- if (BuildConfig .DEBUG ) {Log .e (LOG_TAG , jsonObject .get ("message" ).toString ());}
152
- } catch (IOException e ) {
153
- if (BuildConfig .DEBUG ) {Log .e (LOG_TAG , "" + e .getMessage ());}
154
- }
155
- }
156
- }
184
+ }
0 commit comments