@@ -47,32 +47,34 @@ public class TokenHelper {
47
47
static final boolean mDebug = BuildConfig .DEBUG ;
48
48
49
49
@ Nullable
50
- public static String getAccessToken (@ NonNull Activity activity ) {
51
- AccountManager accountManager = AccountManager .get (activity );
50
+ public static String getAccessToken (@ NonNull Context context ) {
51
+ AccountManager accountManager = AccountManager .get (context );
52
52
Account account = getAccount (accountManager , 0 );
53
53
if (account != null ) {
54
54
/* Default: Load the access token from AccountManager. */
55
55
return accountManager .getUserData (account , "token" );
56
56
} else if (mDebug ) {
57
57
/* Debug: Try to load and validate the access token. */
58
- return loadTokenFromPackageMeta (activity , accountManager );
58
+ return loadTokenFromPackageMeta (context , accountManager );
59
59
} else {
60
60
Log .e (LOG_TAG , "Account not found: " + Constants .ACCOUNT_TYPE );
61
61
return null ;
62
62
}
63
63
}
64
64
65
- public static void setAccessToken (Activity activity , @ Nullable String token ) {
66
- AccountManager accountManager = AccountManager .get (activity );
65
+ public static void setAccessToken (Context context , @ Nullable String token ) {
66
+ AccountManager accountManager = AccountManager .get (context );
67
67
Account account = getAccount (accountManager , 0 );
68
68
if (account != null && token == null ) {
69
- accountManager .removeAccount (account , activity , accountManagerFuture -> {
70
- Log .d (LOG_TAG , "Account removed: " + Constants .ACCOUNT_TYPE );
71
- }, new Handler (Looper .getMainLooper ()));
69
+ if (context instanceof Activity activity ) {
70
+ accountManager .removeAccount (account , activity , accountManagerFuture -> {
71
+ Log .d (LOG_TAG , "Account removed: " + Constants .ACCOUNT_TYPE );
72
+ }, new Handler (Looper .getMainLooper ()));
73
+ }
72
74
} else if (account == null && token == null ) {
73
75
/* This maybe happen when the token loaded from package-meta has expired. */
74
- Intent intent = new Intent (activity , AuthenticatorActivity .class );
75
- activity .startActivity (intent );
76
+ Intent intent = new Intent (context , AuthenticatorActivity .class );
77
+ context .startActivity (intent );
76
78
}
77
79
}
78
80
@@ -91,16 +93,16 @@ public static String getUsername(@NonNull Context context) {
91
93
}
92
94
93
95
@ Nullable
94
- private static String loadTokenFromPackageMeta (@ NonNull Activity activity , AccountManager accountManager ) {
96
+ private static String loadTokenFromPackageMeta (@ NonNull Context context , AccountManager accountManager ) {
95
97
String token = null ;
96
98
try {
97
99
98
100
ApplicationInfo app ;
99
101
if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .TIRAMISU ) {
100
102
PackageManager .ApplicationInfoFlags flags = PackageManager .ApplicationInfoFlags .of (PackageManager .GET_META_DATA );
101
- app = activity .getPackageManager ().getApplicationInfo (activity .getPackageName (), flags );
103
+ app = context .getPackageManager ().getApplicationInfo (context .getPackageName (), flags );
102
104
} else {
103
- app = activity .getPackageManager ().getApplicationInfo (activity .getPackageName (), PackageManager .GET_META_DATA );
105
+ app = context .getPackageManager ().getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
104
106
}
105
107
106
108
if (mDebug && app .metaData .keySet ().contains ("com.github.ACCESS_TOKEN" )) {
@@ -134,9 +136,9 @@ public void onResponse(@NonNull Call<User> call, @NonNull Response<User> respons
134
136
if (message .equals ("\" Bad credentials\" " )) {
135
137
136
138
// Remove the token, it is invalid anyway.
137
- TokenHelper .setAccessToken (activity , null );
139
+ TokenHelper .setAccessToken (context , null );
138
140
139
- if (activity instanceof NavHostActivity activity2 ) {
141
+ if (context instanceof NavHostActivity activity2 ) {
140
142
if (activity2 .getCurrentFragment () instanceof HomeScreenFragment ) {
141
143
activity2 .getNavController ().navigate (
142
144
R .id .action_homeScreenFragment_to_preferencesFragment
0 commit comments