mirror of
https://github.com/whyorean/AuroraStore.git
synced 2026-06-18 20:48:35 -04:00
Fix api building, when session expires
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Aurora Store
|
||||
* Copyright (C) 2019, Rahul Kumar Patel <whyorean@gmail.com>
|
||||
*
|
||||
* Yalp Store
|
||||
* Copyright (C) 2018 Sergey Yeriomin <yeriomin@gmail.com>
|
||||
*
|
||||
* Aurora Store is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Aurora Store is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package com.aurora.store.api;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.aurora.store.TokenDispenserMirrors;
|
||||
import com.aurora.store.model.LoginInfo;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.dragons.aurora.playstoreapiv2.GooglePlayAPI;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class PlayStoreApiAuthenticator {
|
||||
|
||||
public static GooglePlayAPI login(Context context) throws IOException {
|
||||
LoginInfo loginInfo = new LoginInfo();
|
||||
loginInfo.setTokenDispenserUrl(TokenDispenserMirrors.get(context));
|
||||
return ApiBuilderUtil.buildApi(context, loginInfo, true);
|
||||
}
|
||||
|
||||
public static GooglePlayAPI getApi(Context context) throws Exception {
|
||||
return ApiBuilderUtil.buildFromPreferences(context);
|
||||
}
|
||||
|
||||
public GooglePlayAPI getPlayApi(Context context) throws Exception {
|
||||
return ApiBuilderUtil.buildFromPreferences(context);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,13 +8,13 @@ import android.content.Intent;
|
||||
import com.aurora.store.AuroraApplication;
|
||||
import com.aurora.store.Constants;
|
||||
import com.aurora.store.R;
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.model.App;
|
||||
import com.aurora.store.notification.QuickNotification;
|
||||
import com.aurora.store.task.LiveUpdate;
|
||||
import com.aurora.store.task.ObservableDeliveryData;
|
||||
import com.aurora.store.task.UpdatableAppsTask;
|
||||
import com.aurora.store.ui.main.AuroraActivity;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.aurora.store.util.Log;
|
||||
import com.aurora.store.util.TextUtil;
|
||||
import com.aurora.store.util.Util;
|
||||
@@ -37,7 +37,7 @@ public class UpdatesReceiver extends BroadcastReceiver {
|
||||
this.context = context;
|
||||
Log.i("Update check Started");
|
||||
|
||||
disposable.add(Observable.fromCallable(() -> new PlayStoreApiAuthenticator().getPlayApi(context))
|
||||
disposable.add(Observable.fromCallable(() -> ApiBuilderUtil.getPlayApi(context))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(api -> new UpdatableAppsTask(AuroraApplication.api, context).getUpdatableApps())
|
||||
.subscribe(apps -> {
|
||||
|
||||
@@ -23,8 +23,8 @@ package com.aurora.store.task;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.model.App;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.dragons.aurora.playstoreapiv2.GooglePlayAPI;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -43,7 +43,7 @@ public abstract class BaseTask extends ContextWrapper {
|
||||
}
|
||||
|
||||
public GooglePlayAPI getApi() throws Exception {
|
||||
return PlayStoreApiAuthenticator.getApi(context);
|
||||
return ApiBuilderUtil.getApi(context);
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
|
||||
@@ -42,9 +42,9 @@ import androidx.fragment.app.Fragment;
|
||||
import com.aurora.store.AuroraApplication;
|
||||
import com.aurora.store.GlideApp;
|
||||
import com.aurora.store.R;
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.ui.single.activity.GoogleLoginActivity;
|
||||
import com.aurora.store.util.Accountant;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.aurora.store.util.ContextUtil;
|
||||
import com.aurora.store.util.Log;
|
||||
import com.aurora.store.util.NetworkUtil;
|
||||
@@ -145,7 +145,7 @@ public class AccountsFragment extends Fragment {
|
||||
@OnClick(R.id.btn_anonymous)
|
||||
public void loginAnonymous() {
|
||||
if (NetworkUtil.isConnected(requireContext())) {
|
||||
disposable.add(Observable.fromCallable(() -> PlayStoreApiAuthenticator
|
||||
disposable.add(Observable.fromCallable(() -> ApiBuilderUtil
|
||||
.login(requireContext()))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(api -> {
|
||||
|
||||
@@ -40,13 +40,12 @@ import androidx.navigation.Navigation;
|
||||
|
||||
import com.aurora.store.Constants;
|
||||
import com.aurora.store.R;
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.ui.main.AuroraActivity;
|
||||
import com.aurora.store.ui.preference.SettingsActivity;
|
||||
import com.aurora.store.ui.single.activity.BaseActivity;
|
||||
import com.aurora.store.ui.single.activity.GoogleLoginActivity;
|
||||
import com.aurora.store.ui.single.activity.SplashActivity;
|
||||
import com.aurora.store.util.Accountant;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.aurora.store.util.ContextUtil;
|
||||
import com.aurora.store.util.NetworkUtil;
|
||||
import com.aurora.store.util.PrefUtil;
|
||||
@@ -183,7 +182,7 @@ public class IntroActivity extends BaseActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
Disposable disposable = Observable.fromCallable(() -> PlayStoreApiAuthenticator
|
||||
Disposable disposable = Observable.fromCallable(() -> ApiBuilderUtil
|
||||
.login(this))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
@@ -8,6 +8,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatImageView;
|
||||
import androidx.appcompat.widget.AppCompatTextView;
|
||||
import androidx.work.Constraints;
|
||||
import androidx.work.Data;
|
||||
import androidx.work.NetworkType;
|
||||
import androidx.work.OneTimeWorkRequest;
|
||||
import androidx.work.WorkManager;
|
||||
@@ -78,9 +79,16 @@ public class SplashActivity extends BaseActivity {
|
||||
case ENQUEUED:
|
||||
status.setText(R.string.toast_api_build_api);
|
||||
break;
|
||||
|
||||
case FAILED:
|
||||
status.setText(R.string.toast_api_build_failed);
|
||||
Data data = workInfo.getOutputData();
|
||||
int errorCode = data.getInt(ApiValidator.ERROR_KEY, -1);
|
||||
if (errorCode == 0) {
|
||||
status.setText(R.string.toast_login_failed);
|
||||
NavigationUtil.launchAccountsActivity(this);
|
||||
} else if (errorCode == 1)
|
||||
status.setText(R.string.toast_api_build_failed);
|
||||
else if (errorCode == 2)
|
||||
status.setText(R.string.error_no_network);
|
||||
break;
|
||||
|
||||
case SUCCEEDED:
|
||||
|
||||
@@ -5,8 +5,8 @@ import android.content.SharedPreferences;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.aurora.store.Constants;
|
||||
import com.aurora.store.TokenDispenserMirrors;
|
||||
import com.aurora.store.adapter.OkHttpClientAdapter;
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.exception.CredentialsEmptyException;
|
||||
import com.aurora.store.manager.LocaleManager;
|
||||
import com.aurora.store.manager.SpoofManager;
|
||||
@@ -39,7 +39,7 @@ public class ApiBuilderUtil {
|
||||
GooglePlayAPI api;
|
||||
LoginInfo loginInfo;
|
||||
if (Accountant.isAnonymous(context)) {
|
||||
api = PlayStoreApiAuthenticator.login(context);
|
||||
api = login(context);
|
||||
loginInfo = LoginInfo.getSavedInstance(context);
|
||||
} else {
|
||||
loginInfo = LoginInfo.getSavedInstance(context);
|
||||
@@ -116,4 +116,18 @@ public class ApiBuilderUtil {
|
||||
return deviceInfoProvider;
|
||||
}
|
||||
|
||||
public static GooglePlayAPI login(Context context) throws IOException {
|
||||
LoginInfo loginInfo = new LoginInfo();
|
||||
loginInfo.setTokenDispenserUrl(TokenDispenserMirrors.get(context));
|
||||
return buildApi(context, loginInfo, true);
|
||||
}
|
||||
|
||||
public static GooglePlayAPI getApi(Context context) throws Exception {
|
||||
return buildFromPreferences(context);
|
||||
}
|
||||
|
||||
public static GooglePlayAPI getPlayApi(Context context) throws Exception {
|
||||
return buildFromPreferences(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,18 +3,34 @@ package com.aurora.store.worker;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.work.Data;
|
||||
import androidx.work.Worker;
|
||||
import androidx.work.WorkerParameters;
|
||||
|
||||
import com.aurora.store.AuroraApplication;
|
||||
import com.aurora.store.api.PlayStoreApiAuthenticator;
|
||||
import com.aurora.store.exception.CredentialsEmptyException;
|
||||
import com.aurora.store.exception.TooManyRequestsException;
|
||||
import com.aurora.store.util.ApiBuilderUtil;
|
||||
import com.aurora.store.util.Log;
|
||||
import com.dragons.aurora.playstoreapiv2.AuthException;
|
||||
import com.dragons.aurora.playstoreapiv2.GooglePlayAPI;
|
||||
import com.dragons.aurora.playstoreapiv2.TocResponse;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class ApiValidator extends Worker {
|
||||
|
||||
public static final String TAG = "API_VALIDATOR";
|
||||
public static final String ERROR_KEY = "ERROR_KEY";
|
||||
|
||||
/*
|
||||
* -1 - No error
|
||||
* 0 - Credentials Empty
|
||||
* 1 - Too many requests/Session expired
|
||||
* 2 - Network Error
|
||||
* 3 - Unknown
|
||||
*/
|
||||
private int errorCode = -1;
|
||||
|
||||
public ApiValidator(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
||||
super(context, workerParams);
|
||||
@@ -22,8 +38,23 @@ public class ApiValidator extends Worker {
|
||||
}
|
||||
|
||||
private GooglePlayAPI buildApi() throws Exception {
|
||||
final PlayStoreApiAuthenticator apiAuthenticator = new PlayStoreApiAuthenticator();
|
||||
return apiAuthenticator.getPlayApi(getApplicationContext());
|
||||
try {
|
||||
return ApiBuilderUtil.getPlayApi(getApplicationContext());
|
||||
} catch (Exception e) {
|
||||
if (e instanceof CredentialsEmptyException) {
|
||||
errorCode = 0;
|
||||
return null;
|
||||
} else if (e instanceof AuthException || e instanceof TooManyRequestsException) {
|
||||
errorCode = 1;
|
||||
return ApiBuilderUtil.generateApiWithNewAuthToken(getApplicationContext());
|
||||
} else if (e instanceof UnknownHostException) {
|
||||
errorCode = 2;
|
||||
return null;
|
||||
} else {
|
||||
errorCode = 3;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean testApi(GooglePlayAPI api) throws Exception {
|
||||
@@ -31,20 +62,31 @@ public class ApiValidator extends Worker {
|
||||
return tocResponse.hasTosToken();
|
||||
}
|
||||
|
||||
private Data getOutputData() {
|
||||
return new Data.Builder()
|
||||
.putInt(ERROR_KEY, errorCode)
|
||||
.build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Result doWork() {
|
||||
try {
|
||||
GooglePlayAPI googlePlayAPI = buildApi();
|
||||
|
||||
if (googlePlayAPI == null) {
|
||||
|
||||
return Result.failure(getOutputData());
|
||||
}
|
||||
|
||||
if (testApi(googlePlayAPI)) {
|
||||
AuroraApplication.api = googlePlayAPI;
|
||||
return Result.success();
|
||||
} else {
|
||||
return Result.failure();
|
||||
return Result.failure(getOutputData());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(e.getMessage());
|
||||
return Result.failure();
|
||||
return Result.failure(getOutputData());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user