From 6c1845e246b29146b47f9acc3df7565045cb25a2 Mon Sep 17 00:00:00 2001 From: Aunali321 Date: Tue, 11 Oct 2022 20:19:50 +0530 Subject: [PATCH] feat: add fallback for cronet. --- lib/services/github_api.dart | 28 +++++++++++++++++----------- lib/services/revanced_api.dart | 24 +++++++++++++++--------- lib/utils/check_for_gms.dart | 10 ++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 lib/utils/check_for_gms.dart diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index b38fad42..fbdbbc77 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -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 clearAllCache() async { diff --git a/lib/services/revanced_api.dart b/lib/services/revanced_api.dart index dab0cfc6..1eace1db 100644 --- a/lib/services/revanced_api.dart +++ b/lib/services/revanced_api.dart @@ -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 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 clearAllCache() async { diff --git a/lib/utils/check_for_gms.dart b/lib/utils/check_for_gms.dart new file mode 100644 index 00000000..79474eae --- /dev/null +++ b/lib/utils/check_for_gms.dart @@ -0,0 +1,10 @@ +// Check for google mobile services on device + +import 'package:device_apps/device_apps.dart'; + +Future checkForGMS() async { + bool isGMSInstalled = true; + isGMSInstalled = await DeviceApps.isAppInstalled('com.google.android.gms') || + await DeviceApps.isAppInstalled('com.android.vending'); + return isGMSInstalled; +}