Skip to content

Commit

Permalink
fix rhymelph#76,support android 13, release v0.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyme committed Mar 10, 2023
1 parent dcc9710 commit 99b8282
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 0.4.2
* fix issues(#76) support android 13.
## 0.4.1
* fix issues(#71)
## 0.4.0
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
compileSdkVersion 33

defaultConfig {
minSdkVersion 16
Expand Down
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<!-- <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />-->
<application android:usesCleartextTraffic="true">
Expand Down
12 changes: 6 additions & 6 deletions android/src/main/java/com/example/r_upgrade/RUpgradePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import androidx.annotation.NonNull;

import com.example.r_upgrade.common.StoragePermissions;
import com.example.r_upgrade.common.DownloadPermissions;
import com.example.r_upgrade.common.manager.UpgradeManager;
import com.example.r_upgrade.common.UpgradeService;
import com.example.r_upgrade.method.RUpgradeMethodCallHandler;
Expand All @@ -32,13 +32,13 @@ public RUpgradePlugin() {

}

private RUpgradePlugin(Activity activity, BinaryMessenger messenger, StoragePermissions.PermissionsRegistry permissionsRegistry) {
private RUpgradePlugin(Activity activity, BinaryMessenger messenger, DownloadPermissions.PermissionsRegistry permissionsRegistry) {
initPlugin(activity, messenger, permissionsRegistry);
}

private void initPlugin(Activity activity, BinaryMessenger messenger, StoragePermissions.PermissionsRegistry permissionsRegistry) {
private void initPlugin(Activity activity, BinaryMessenger messenger, DownloadPermissions.PermissionsRegistry permissionsRegistry) {
_channel = new MethodChannel(messenger, PLUGIN_METHOD_NAME);
upgradeManager = new UpgradeManager(activity, _channel, new StoragePermissions(), permissionsRegistry);
upgradeManager = new UpgradeManager(activity, _channel, new DownloadPermissions(), permissionsRegistry);
_channel.setMethodCallHandler(new RUpgradeMethodCallHandler(upgradeManager));
}

Expand All @@ -47,7 +47,7 @@ private void initPlugin(Activity activity, BinaryMessenger messenger, StoragePer
*/
public static void registerWith(final Registrar registrar) {

new RUpgradePlugin(registrar.activity(), registrar.messenger(), new StoragePermissions.PermissionsRegistry() {
new RUpgradePlugin(registrar.activity(), registrar.messenger(), new DownloadPermissions.PermissionsRegistry() {
@Override
public void addListener(PluginRegistry.RequestPermissionsResultListener handler) {
registrar.addRequestPermissionsResultListener(handler);
Expand All @@ -71,7 +71,7 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

@Override
public void onAttachedToActivity(@NonNull final ActivityPluginBinding binding) {
initPlugin(binding.getActivity(), flutterPluginBinding.getBinaryMessenger(), new StoragePermissions.PermissionsRegistry() {
initPlugin(binding.getActivity(), flutterPluginBinding.getBinaryMessenger(), new DownloadPermissions.PermissionsRegistry() {
@Override
public void addListener(PluginRegistry.RequestPermissionsResultListener handler) {
binding.addRequestPermissionsResultListener(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import io.flutter.plugin.common.PluginRegistry;

public class StoragePermissions {
public class DownloadPermissions {
public interface PermissionsRegistry {
void addListener(PluginRegistry.RequestPermissionsResultListener handler);
}
Expand All @@ -25,11 +27,35 @@ public interface ResultCallback {
public void requestPermissions(
Activity activity,
PermissionsRegistry permissionsRegistry,
Integer notificationVisibility,
final ResultCallback callback) {
if (ongoing) {
callback.onResult("storagePermission", "Read/Write External Storage permission request ongoing");
}
if (!hasReadStoragePermission(activity) || !hasWritePermission(activity)) {

if (Build.VERSION.SDK_INT >= 33) {
if (ongoing) {
callback.onResult("downloadPermission", "Notification permission request ongoing");
}
if (notificationVisibility != 2&&!hasNotificationPermission(activity)) {
permissionsRegistry.addListener(
new StorageRequestPermissionsListener(new ResultCallback() {
@Override
public void onResult(String errorCode, String errorDescription) {
ongoing = false;
callback.onResult(errorCode, errorDescription);
}
}));
ongoing = true;
ActivityCompat.requestPermissions(
activity,
new String[]{Manifest.permission.POST_NOTIFICATIONS},
STORAGE_REQUEST_ID);
} else {
// Permissions already exist. Call the callback with success.
callback.onResult(null, null);
}
} else if (Build.VERSION.SDK_INT < 30 && (!hasReadStoragePermission(activity) || !hasWritePermission(activity))) {
if (ongoing) {
callback.onResult("downloadPermission", "Read/Write External Storage permission request ongoing");
}
permissionsRegistry.addListener(
new StorageRequestPermissionsListener(new ResultCallback() {
@Override
Expand All @@ -49,6 +75,12 @@ public void onResult(String errorCode, String errorDescription) {
}
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
private boolean hasNotificationPermission(Activity activity) {
return ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED;
}

private boolean hasReadStoragePermission(Activity activity) {
return ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
Expand Down Expand Up @@ -81,14 +113,27 @@ public boolean onRequestPermissionsResult(int id, String[] permissions, int[] gr
if (alreadyCalled || id != STORAGE_REQUEST_ID) {
return false;
}
if (grantResults.length != 2) return false;
alreadyCalled = true;
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
callback.onResult("storagePermission", "Read/Write External Storage permission not granted");
if (Build.VERSION.SDK_INT >= 33) {
if (grantResults.length != 1) return false;
alreadyCalled = true;

if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
callback.onResult("downloadPermission", "Notification permission not granted");
} else {
callback.onResult(null, null);
}
return true;
} else {
callback.onResult(null, null);
if (grantResults.length != 2) return false;
alreadyCalled = true;

if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
callback.onResult("downloadPermission", "Read/Write External Storage permission not granted");
} else {
callback.onResult(null, null);
}
return true;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.example.r_upgrade.common.DownloadStatus;
import com.example.r_upgrade.common.RUpgradeLogger;
import com.example.r_upgrade.common.ResultMap;
import com.example.r_upgrade.common.StoragePermissions;
import com.example.r_upgrade.common.DownloadPermissions;
import com.example.r_upgrade.common.UpgradeNotification;
import com.example.r_upgrade.common.UpgradeNotificationStyle;
import com.example.r_upgrade.common.UpgradeSQLite;
Expand Down Expand Up @@ -87,19 +87,19 @@ public class UpgradeManager extends ContextWrapper {

private MethodChannel channel;

private StoragePermissions.PermissionsRegistry permissionsRegistry;
private StoragePermissions storagePermissions;
private DownloadPermissions.PermissionsRegistry permissionsRegistry;
private DownloadPermissions downloadPermissions;
private Activity activity;


public void dispose() {
unregisterReceiver(downloadReceiver);
}

public UpgradeManager(Activity base, MethodChannel channel, StoragePermissions storagePermissions, StoragePermissions.PermissionsRegistry permissionsRegistry) {
public UpgradeManager(Activity base, MethodChannel channel, DownloadPermissions storagePermissions, DownloadPermissions.PermissionsRegistry permissionsRegistry) {
super(base);
this.activity = base;
this.storagePermissions = storagePermissions;
this.downloadPermissions = storagePermissions;
this.permissionsRegistry = permissionsRegistry;
this.channel = channel;
UpgradeSQLite.getInstance(this).pauseDownloading();
Expand All @@ -122,7 +122,7 @@ public void upgrade(final String url, final Map<String, String> header, final St
}
this.notificationVisibility = notificationVisibility;

storagePermissions.requestPermissions(activity, permissionsRegistry, new StoragePermissions.ResultCallback() {
downloadPermissions.requestPermissions(activity, permissionsRegistry, notificationVisibility,new DownloadPermissions.ResultCallback() {
@Override
public void onResult(String errorCode, String errorDescription) {
if (errorCode == null) {
Expand Down Expand Up @@ -325,7 +325,7 @@ public void installApkById(final int id, int installType, final MethodChannel.Re
installFactory = installTypeToFactory(installType);
}
if (installFactory == null) return;
storagePermissions.requestPermissions(activity, permissionsRegistry, new StoragePermissions.ResultCallback() {
downloadPermissions.requestPermissions(activity, permissionsRegistry,notificationVisibility, new DownloadPermissions.ResultCallback() {
@Override
public void onResult(String errorCode, String errorDescription) {
if (errorCode == null) {
Expand Down Expand Up @@ -355,7 +355,7 @@ private BaseInstallFactory installTypeToFactory(int installType) {
public void installApkByPath(final String path, final int upgradeFlavor, int installType, final MethodChannel.Result result) {
installFactory = installTypeToFactory(installType);

storagePermissions.requestPermissions(activity, permissionsRegistry, new StoragePermissions.ResultCallback() {
downloadPermissions.requestPermissions(activity, permissionsRegistry,notificationVisibility, new DownloadPermissions.ResultCallback() {
@Override
public void onResult(String errorCode, String errorDescription) {
if (errorCode == null) {
Expand Down Expand Up @@ -473,7 +473,7 @@ public void upgradeWithId(final Integer id, Integer notificationVisibility, int
int status = (int) result.get(UpgradeSQLite.STATUS);
if (status == DownloadStatus.STATUS_PAUSED.getValue() || status == DownloadStatus.STATUS_FAILED.getValue()
|| status == DownloadStatus.STATUS_CANCEL.getValue() || !downloadFile.exists()) {
storagePermissions.requestPermissions(activity, permissionsRegistry, new StoragePermissions.ResultCallback() {
downloadPermissions.requestPermissions(activity, permissionsRegistry, notificationVisibility,new DownloadPermissions.ResultCallback() {
@Override
public void onResult(String errorCode, String errorDescription) {
if (errorCode == null) {
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -37,7 +37,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.r_upgrade_example"
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
24 changes: 24 additions & 0 deletions example/lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ class MessageLookup extends MessageLookupByLibrary {
"Look at the last time ID download status"),
"No_ID_last_time_to_download":
MessageLookupByLibrary.simpleMessage("No ID last time to download"),
"Notification_Related":
MessageLookupByLibrary.simpleMessage("Notification Related"),
"Notification_Style":
MessageLookupByLibrary.simpleMessage("Notification Style"),
"Notification_Style_None": MessageLookupByLibrary.simpleMessage("None"),
"Notification_Style_PlanTime":
MessageLookupByLibrary.simpleMessage("PlanTime"),
"Notification_Style_PlanTime_Speech":
MessageLookupByLibrary.simpleMessage("PlanTime/Speech"),
"Notification_Style_Speech":
MessageLookupByLibrary.simpleMessage("Speech"),
"Notification_Style_Speech_PlanTime":
MessageLookupByLibrary.simpleMessage("Speech/PlanTime"),
"Notification_Visibility":
MessageLookupByLibrary.simpleMessage("Notification Visibility"),
"Notification_Visibility_Hidden":
MessageLookupByLibrary.simpleMessage("Hidden"),
"Notification_Visibility_Visible":
MessageLookupByLibrary.simpleMessage("Visible"),
"Notification_Visibility_Visible_Notify_Completed":
MessageLookupByLibrary.simpleMessage("Visible Notify Completed"),
"Notification_Visibility_Visible_Notify_Only_Completion":
MessageLookupByLibrary.simpleMessage(
"Visible Notify Only Completion"),
"Please_click_on_start_hot_update":
MessageLookupByLibrary.simpleMessage(
"Please click on start hot update"),
Expand Down
21 changes: 21 additions & 0 deletions example/lib/generated/intl/messages_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("查看最后一次ID的下载状态"),
"No_ID_last_time_to_download":
MessageLookupByLibrary.simpleMessage("没有最后一次下载的ID"),
"Notification_Related": MessageLookupByLibrary.simpleMessage("通知相关"),
"Notification_Style": MessageLookupByLibrary.simpleMessage("通知样式"),
"Notification_Style_None": MessageLookupByLibrary.simpleMessage("无"),
"Notification_Style_PlanTime":
MessageLookupByLibrary.simpleMessage("预计时间"),
"Notification_Style_PlanTime_Speech":
MessageLookupByLibrary.simpleMessage("预计时间/下载速度"),
"Notification_Style_Speech":
MessageLookupByLibrary.simpleMessage("下载速度"),
"Notification_Style_Speech_PlanTime":
MessageLookupByLibrary.simpleMessage("下载速度/预计时间"),
"Notification_Visibility":
MessageLookupByLibrary.simpleMessage("通知栏可见"),
"Notification_Visibility_Hidden":
MessageLookupByLibrary.simpleMessage("隐藏显示"),
"Notification_Visibility_Visible":
MessageLookupByLibrary.simpleMessage("总是显示"),
"Notification_Visibility_Visible_Notify_Completed":
MessageLookupByLibrary.simpleMessage("显示直到下载完成"),
"Notification_Visibility_Visible_Notify_Only_Completion":
MessageLookupByLibrary.simpleMessage("下载完成才显示"),
"Please_click_on_start_hot_update":
MessageLookupByLibrary.simpleMessage("请点击开始热更新"),
"Please_click_on_start_incremental_updates":
Expand Down
Loading

0 comments on commit 99b8282

Please sign in to comment.