Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Android 14 #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 33
compileSdkVersion 34

Copy link

@loonix loonix Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you missed adding the namespace

Suggested change
namespace 'com.example.r_upgrade'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it helps a lot , thank you

defaultConfig {
minSdkVersion 16
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<!-- <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />-->
<application android:usesCleartextTraffic="true">
<service android:name="com.example.r_upgrade.common.UpgradeService">
<service android:name="com.example.r_upgrade.common.UpgradeService" android:exported="true">

</service>
<provider
Expand Down
49 changes: 9 additions & 40 deletions android/src/main/java/com/example/r_upgrade/RUpgradePlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.r_upgrade;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;
Expand All @@ -13,10 +12,7 @@
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* RUpgradePlugin
Expand All @@ -28,38 +24,14 @@ public class RUpgradePlugin implements FlutterPlugin, ActivityAware {
private UpgradeManager upgradeManager;
private FlutterPluginBinding flutterPluginBinding;

public RUpgradePlugin() {

}

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

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

/**
* Plugin registration.
*/
public static void registerWith(final Registrar registrar) {

new RUpgradePlugin(registrar.activity(), registrar.messenger(), new DownloadPermissions.PermissionsRegistry() {
@Override
public void addListener(PluginRegistry.RequestPermissionsResultListener handler) {
registrar.addRequestPermissionsResultListener(handler);
}
});
}


@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
this.flutterPluginBinding = binding;

_channel = new MethodChannel(binding.getBinaryMessenger(), PLUGIN_METHOD_NAME);
upgradeManager = new UpgradeManager(
binding.getApplicationContext(), _channel, new DownloadPermissions()
);
_channel.setMethodCallHandler(new RUpgradeMethodCallHandler(upgradeManager));
}

@Override
Expand All @@ -68,15 +40,12 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
flutterPluginBinding = null;
}


@Override
public void onAttachedToActivity(@NonNull final ActivityPluginBinding binding) {
initPlugin(binding.getActivity(), flutterPluginBinding.getBinaryMessenger(), new DownloadPermissions.PermissionsRegistry() {
@Override
public void addListener(PluginRegistry.RequestPermissionsResultListener handler) {
binding.addRequestPermissionsResultListener(handler);
}
});
if (upgradeManager != null) {
upgradeManager.setActivity(binding.getActivity());
upgradeManager.setPermissionsRegistry(binding::addRequestPermissionsResultListener);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ public void onLost(Network network) {
} else {
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
}
registerReceiver(actionReceiver, filter);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(actionReceiver, filter, Context.RECEIVER_EXPORTED);
} else {
registerReceiver(actionReceiver, filter);
}
}

private void handleNetworkChange(boolean isConnected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
Expand Down Expand Up @@ -82,36 +83,46 @@ public class UpgradeManager extends ContextWrapper {
private Integer notificationVisibility = 0;
private UpgradeNotificationStyle notificationStyle = UpgradeNotificationStyle.none;

final private BroadcastReceiver downloadReceiver;

private BroadcastReceiver downloadReceiver;

private MethodChannel channel;
final private MethodChannel channel;

private DownloadPermissions.PermissionsRegistry permissionsRegistry;
private DownloadPermissions downloadPermissions;

public void setPermissionsRegistry(DownloadPermissions.PermissionsRegistry permissionsRegistry) {
this.permissionsRegistry = permissionsRegistry;
}

final private DownloadPermissions downloadPermissions;

private Activity activity;

public void setActivity(Activity activity) {
this.activity = activity;
}

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

public UpgradeManager(Activity base, MethodChannel channel, DownloadPermissions storagePermissions, DownloadPermissions.PermissionsRegistry permissionsRegistry) {
super(base);
this.activity = base;
public UpgradeManager(Context context, MethodChannel channel, DownloadPermissions storagePermissions) {
super(context);
this.downloadPermissions = storagePermissions;
this.permissionsRegistry = permissionsRegistry;
this.channel = channel;
UpgradeSQLite.getInstance(this).pauseDownloading();
IntentFilter filter = new IntentFilter();
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
filter.addAction(UpgradeManager.DOWNLOAD_STATUS);
filter.addAction(UpgradeManager.DOWNLOAD_INSTALL);
downloadReceiver = createBroadcastReceiver();
registerReceiver(downloadReceiver, filter);
final int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(downloadReceiver, filter, Context.RECEIVER_EXPORTED);
} else {
registerReceiver(downloadReceiver, filter);
}
}


public void upgrade(final String url, final Map<String, String> header, final String apkName, final Integer notificationVisibility, Integer notificationStyle, Integer installType, Boolean useDownloadManager, final Integer upgradeFlavor, final MethodChannel.Result result) {
installFactory = installTypeToFactory(installType);
this.isUseDownloadManager = Boolean.TRUE == useDownloadManager;
Expand Down Expand Up @@ -384,14 +395,15 @@ public void onReceive(Context context, Intent intent) {
if (packageName == null || !packageName.equals(getPackageName())) {
return;
}
if (intent != null && intent.getAction() != null && intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
final String action = intent.getAction();
if (action != null && action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
if (timer != null) {
timer.cancel();
timer = null;
}
long id = intent.getLongExtra("extra_download_id", 0);
queryTask(id);
} else if (intent != null && intent.getAction() != null && intent.getAction().equals(UpgradeManager.DOWNLOAD_STATUS)) {
} else if (action != null && action.equals(UpgradeManager.DOWNLOAD_STATUS)) {

final long current_length = intent.getLongExtra(PARAMS_CURRENT_LENGTH, 0);
final long max_length = intent.getLongExtra(PARAMS_MAX_LENGTH, 0);
Expand Down Expand Up @@ -435,7 +447,7 @@ public void onReceive(Context context, Intent intent) {
.put(PARAMS_PATH, path)
.getMap());

} else if (intent != null && intent.getAction() != null && intent.getAction().equals(UpgradeManager.DOWNLOAD_INSTALL)) {
} else if (action != null && action.equals(UpgradeManager.DOWNLOAD_INSTALL)) {
int id = intent.getIntExtra(UpgradeService.DOWNLOAD_ID, 0);
installApkById(id);
UpgradeNotification.removeNotification(context, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.example.r_upgrade.common.manager.UpgradeManager;

import io.flutter.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

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 33
compileSdkVersion 34

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 33
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
5 changes: 4 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import 'generated/l10n.dart';

const version = 1;

void main() => runApp(Application());
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(Application());
}

enum UpgradeMethod {
all,
Expand Down
Loading