Skip to content

Commit

Permalink
fix < android q save
Browse files Browse the repository at this point in the history
  • Loading branch information
Notsfsssf committed Mar 16, 2021
1 parent ca4a969 commit a5d3108
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 45 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ android {
applicationId packageName
minSdkVersion 21
targetSdkVersion 30
versionCode 1000402
versionName "0.4.2X"
versionCode 10004030
versionName "0.4.3 Impl"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
56 changes: 38 additions & 18 deletions android/app/src/main/kotlin/com/perol/pixez/Imager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,70 @@ package com.perol.pixez
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.webkit.MimeTypeMap
import java.io.File
import java.io.OutputStream

fun Context.save(byteArray: ByteArray, name: String) {
fun Context.save(byteArray: ByteArray, name: String): String? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val dirFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pixez")
if (!dirFile.exists())
dirFile.mkdirs()
val targetFile = File(dirFile.absolutePath + "/" + name)
if (!targetFile.exists())
targetFile.createNewFile()
else
targetFile.delete()
targetFile.outputStream().use {
it.write(byteArray)
}
return targetFile.absolutePath
}
val values = ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, name.split("/").last())
values.put(MediaStore.MediaColumns.MIME_TYPE, if (name.endsWith("png")) {
"image/png"
} else {
"image/jpeg"
})
val displayName = name.split("/").last()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
values.put(MediaStore.MediaColumns.MIME_TYPE, MimeTypeMap.getSingleton().getMimeTypeFromExtension(displayName))

val path = if (name.contains("/")) {
"${Environment.DIRECTORY_PICTURES}/PixEz/${name.split("/").first()}"
"${Environment.DIRECTORY_PICTURES}/pixez/${name.split("/").first()}"
} else {
"${Environment.DIRECTORY_PICTURES}/PixEz"
"${Environment.DIRECTORY_PICTURES}/pixez"
}
values.put(MediaStore.MediaColumns.RELATIVE_PATH, path);
var uri: Uri? = null
var outputStream: OutputStream? = null
try {
uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
outputStream = contentResolver.openOutputStream(uri!!)!!
outputStream.write(byteArray)
outputStream.flush()
outputStream.close()
contentResolver.openOutputStream(uri!!)?.use {
it.write(byteArray)
it.flush()
}
} catch (e: Exception) {
if (uri != null) {
contentResolver.delete(uri, null, null);
}
} finally {
outputStream?.close()
}
return null
}

fun Context.exist(name: String): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
//what the hell
val dirFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pixez")
if (!dirFile.exists())
dirFile.mkdirs()
val targetFile = File(dirFile.absolutePath + "/" + name)
return targetFile.exists()
}
val projection = arrayOf(
MediaStore.Images.Media._ID,
)
val path = if (name.contains("/")) {
"${Environment.DIRECTORY_PICTURES}/PixEz/${name.split("/").first()}"
"${Environment.DIRECTORY_PICTURES}/pixez/${name.split("/").first()}"
} else {
"${Environment.DIRECTORY_PICTURES}/PixEz"
"${Environment.DIRECTORY_PICTURES}/pixez"
}
//想不到吧?居然是这样写?
//咕噜咕噜,这不翻源码写的出来?
Expand Down
19 changes: 16 additions & 3 deletions android/app/src/main/kotlin/com/perol/pixez/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,18 @@ class MainActivity : FlutterActivity() {
clearOld = false
GlobalScope.launch(Dispatchers.Main) {
if (saveMode == 0) {
withContext(Dispatchers.IO) {
val path = withContext(Dispatchers.IO) {
save(data, name)
}
MediaScannerConnection.scanFile(
this@MainActivity,
arrayOf(path),
arrayOf(
MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(File(path).extension)
)
) { _, _ ->
}
} else if (saveMode == 2) {
if (helplessPath == null) {
helplessPath = sharedPreferences.getString("flutter.store_path", null)
Expand Down Expand Up @@ -393,6 +402,7 @@ class MainActivity : FlutterActivity() {
}

private fun encodeGif(name: String, path: String, delay: Int) {

val file = File(path)
file.let {
val tempFile = File(applicationContext.cacheDir, "${
Expand All @@ -404,7 +414,10 @@ class MainActivity : FlutterActivity() {
}.gif")
try {
val fileName = "${name}.gif"

if (saveMode == 0) {
if (exist(fileName))
return
}
/* if (!tempFile.exists()) {
tempFile.createNewFile()
}*/
Expand All @@ -429,7 +442,7 @@ class MainActivity : FlutterActivity() {
} else encoder.encodeFrame(bitmap, delay)
}
encoder.close()
if (saveMode == 1) {
if (saveMode == 0) {
save(tempFile.readBytes(), fileName)
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

class Constants {
static String tagName = "0.4.0";
static String tagName = "0.4.3";

// static bool isGooglePlay =
// bool.fromEnvironment("IS_GOOGLEPLAY", defaultValue: false);//为何没用啊咕鸽?
Expand Down
24 changes: 15 additions & 9 deletions lib/network/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import 'package:dio/dio.dart';
import 'package:dio_http_cache/dio_http_cache.dart';
import 'package:intl/intl.dart';
import 'package:pixez/component/pixiv_image.dart';
import 'package:pixez/main.dart';
import 'package:pixez/models/account.dart';
import 'package:pixez/models/illust_bookmark_tags_response.dart';
import 'package:pixez/models/tags.dart';
import 'package:pixez/models/ugoira_metadata_response.dart';
import 'package:pixez/network/oauth_client.dart';
import 'package:pixez/network/refresh_token_interceptor.dart';

final ApiClient apiClient = ApiClient();
Expand Down Expand Up @@ -83,23 +86,26 @@ class ApiClient {
}
// ..options.connectTimeout = 10000
// ..interceptors.add(LogInterceptor(responseBody: true, requestBody: true))
// ..interceptors.add(CacheInterceptor())
..interceptors.add(DioCacheManager(
CacheConfig(
defaultMaxAge: Duration(days: 1),
defaultRequestMethod: "GET",
skipMemoryCache: true),
).interceptor)
..interceptors.add(RefreshTokenInterceptor());
(httpClient.httpClientAdapter as DefaultHttpClientAdapter)
.onHttpClientCreate = (client) {
HttpClient httpClient = new HttpClient();
httpClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
if (!userSetting.disableBypassSni)
(httpClient.httpClientAdapter as DefaultHttpClientAdapter)
.onHttpClientCreate = (client) {
HttpClient httpClient = new HttpClient();
httpClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
};
return httpClient;
};
return httpClient;
};
else{
httpClient.options.baseUrl = "https://${BASE_API_URL_HOST}";
}
initA(time);
}

Expand Down
21 changes: 13 additions & 8 deletions lib/network/oauth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:intl/intl.dart';
import 'package:pixez/constants.dart';
import 'package:pixez/crypto_plugin.dart';
import 'package:pixez/er/lprinter.dart';
import 'package:pixez/main.dart';

final OAuthClient oAuthClient = OAuthClient();

Expand Down Expand Up @@ -76,15 +77,19 @@ class OAuthClient {
"Host": BASE_OAUTH_URL_HOST
}
..options.contentType = Headers.formUrlEncodedContentType;
(this.httpClient.httpClientAdapter as DefaultHttpClientAdapter)
.onHttpClientCreate = (client) {
HttpClient httpClient = new HttpClient();
httpClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
if (!userSetting.disableBypassSni)
(this.httpClient.httpClientAdapter as DefaultHttpClientAdapter)
.onHttpClientCreate = (client) {
HttpClient httpClient = new HttpClient();
httpClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
};
return httpClient;
};
return httpClient;
};
else {
httpClient.options.baseUrl = "https://${BASE_OAUTH_URL_HOST}";
}
initA(time);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/page/directory/save_mode_choice_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ class _SaveModeChoicePageState extends State<SaveModeChoicePage>
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [Text("MediaStore"), Text(I18n.of(context).media_hint)],
children: [
Text("MediaStore"),
Text(I18n.of(context).media_hint)
],
),
)
],
Expand Down Expand Up @@ -215,7 +218,7 @@ class _SaveModeChoicePageState extends State<SaveModeChoicePage>

Future _onPress(BuildContext context) async {
if (groupValue == 0) {
userSetting.setSaveMode(0);
await userSetting.setSaveMode(0);
Navigator.of(context).pop();
} else if (groupValue == 1) {
await _saffun(context);
Expand Down
18 changes: 18 additions & 0 deletions lib/page/hello/android_hello_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pixez/constants.dart';
import 'package:pixez/custom_icon.dart';
import 'package:pixez/er/leader.dart';
Expand Down Expand Up @@ -194,6 +195,21 @@ class _AndroidHelloPageState extends State<AndroidHelloPage> {
}
}

initPermission() async {
try {
if (Platform.isAndroid && userSetting.saveMode != 1) {
var granted = await Permission.storage.status;
if (!granted.isGranted) {
var b = await Permission.storage.request();
if (!b.isGranted) {
BotToast.showText(text: "storage permission denied");
return;
}
}
}
} catch (e) {}
}

@override
void dispose() {
_intentDataStreamSubscription.cancel();
Expand All @@ -209,6 +225,8 @@ class _AndroidHelloPageState extends State<AndroidHelloPage> {
MaterialPageRoute(builder: (context) => GuidePage()),
(route) => route == null,
);
return;
}
initPermission();
}
}
2 changes: 1 addition & 1 deletion lib/page/picture/illust_lighting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ class _IllustLightingPageState extends State<IllustLightingPage>
List<bool> indexs = [];
bool allOn = false;
for (int i = 0; i < illust.metaPages.length; i++) {
indexs[i] = false;
indexs.add(false);
}
final result = await showDialog(
context: context,
Expand Down
1 change: 0 additions & 1 deletion lib/page/platform/platform_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class _PlatformPageState extends State<PlatformPage> {
onTap: () async {
await showPathDialog(context);
final path = await DocumentPlugin.getPath();
debugPrint(path);
if (mounted) {
setState(() {
this.path = path!;
Expand Down
1 change: 1 addition & 0 deletions lib/store/save_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pixez/document_plugin.dart';
import 'package:pixez/er/toaster.dart';
import 'package:pixez/exts.dart';
Expand Down

0 comments on commit a5d3108

Please sign in to comment.