feat: add fallback for cronet.

This commit is contained in:
Aunali321 2022-10-11 20:19:50 +05:30
parent 0b2572a730
commit 6c1845e246
3 changed files with 42 additions and 20 deletions

View File

@ -2,23 +2,16 @@ import 'dart:convert';
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:dio/dio.dart';
import 'package:dio_brotli_transformer/dio_brotli_transformer.dart';
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
import 'package:native_dio_client/native_dio_client.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/utils/check_for_gms.dart';
@lazySingleton
class GithubAPI {
final Dio _dio = Dio(
BaseOptions(
baseUrl: 'https://api.github.com',
headers: {
'accept-encoding': 'br',
},
),
)..httpClientAdapter = NativeAdapter();
late Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(hours: 6),
@ -35,9 +28,22 @@ class GithubAPI {
'com.spotify.music': 'spotify',
};
void initialize() {
void initialize() async {
bool isGMSInstalled = await checkForGMS();
if (!isGMSInstalled) {
_dio = Dio(BaseOptions(
baseUrl: 'https://api.github.com',
));
print('GitHub API: Using default engine + $isGMSInstalled');
} else {
_dio = Dio(BaseOptions(
baseUrl: 'https://api.github.com',
))
..httpClientAdapter = NativeAdapter();
print('ReVanced API: Using CronetEngine + $isGMSInstalled');
}
_dio.interceptors.add(_dioCacheManager.interceptor);
_dio.transformer = DioBrotliTransformer(transformer: DefaultTransformer());
}
Future<void> clearAllCache() async {

View File

@ -1,12 +1,12 @@
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:dio_brotli_transformer/dio_brotli_transformer.dart';
import 'package:native_dio_client/native_dio_client.dart';
import 'package:dio/dio.dart';
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/utils/check_for_gms.dart';
import 'package:timeago/timeago.dart';
@lazySingleton
@ -19,15 +19,21 @@ class RevancedAPI {
);
Future<void> initialize(String apiUrl) async {
_dio = Dio(BaseOptions(
baseUrl: apiUrl,
headers: {
'accept-encoding': 'br',
},
))
..httpClientAdapter = NativeAdapter();
bool isGMSInstalled = await checkForGMS();
if (!isGMSInstalled) {
_dio = Dio(BaseOptions(
baseUrl: apiUrl,
));
print('ReVanced API: Using default engine + $isGMSInstalled');
} else {
_dio = Dio(BaseOptions(
baseUrl: apiUrl,
))
..httpClientAdapter = NativeAdapter();
print('ReVanced API: Using CronetEngine + $isGMSInstalled');
}
_dio.interceptors.add(_dioCacheManager.interceptor);
_dio.transformer = DioBrotliTransformer(transformer: DefaultTransformer());
}
Future<void> clearAllCache() async {

View File

@ -0,0 +1,10 @@
// Check for google mobile services on device
import 'package:device_apps/device_apps.dart';
Future<bool> checkForGMS() async {
bool isGMSInstalled = true;
isGMSInstalled = await DeviceApps.isAppInstalled('com.google.android.gms') ||
await DeviceApps.isAppInstalled('com.android.vending');
return isGMSInstalled;
}