Showing
4 changed files
with
31 additions
and
22 deletions
| @@ -6,6 +6,7 @@ | @@ -6,6 +6,7 @@ | ||
| 6 | - Implement PdfActionBarTheme for actions bar and add method scrollToPage [Aleksei] | 6 | - Implement PdfActionBarTheme for actions bar and add method scrollToPage [Aleksei] |
| 7 | - Update cursors in zoom mode for web [Aleksei] | 7 | - Update cursors in zoom mode for web [Aleksei] |
| 8 | - Output image sized to cropBox instead of mediaBox (iOS) [garrettApproachableGeek] | 8 | - Output image sized to cropBox instead of mediaBox (iOS) [garrettApproachableGeek] |
| 9 | +- Replace Activity with Context for Service Compatibility (Android) [Heinrich] | ||
| 9 | 10 | ||
| 10 | ## 5.11.1 | 11 | ## 5.11.1 |
| 11 | 12 |
| 1 | package net.nfet.flutter.printing; | 1 | package net.nfet.flutter.printing; |
| 2 | 2 | ||
| 3 | -import android.app.Activity; | 3 | +import android.content.Context; |
| 4 | import android.os.Build; | 4 | import android.os.Build; |
| 5 | import android.print.PrintAttributes; | 5 | import android.print.PrintAttributes; |
| 6 | 6 | ||
| 7 | import androidx.annotation.NonNull; | 7 | import androidx.annotation.NonNull; |
| 8 | import androidx.annotation.RequiresApi; | 8 | import androidx.annotation.RequiresApi; |
| 9 | 9 | ||
| 10 | +import java.lang.ref.WeakReference; | ||
| 10 | import java.util.ArrayList; | 11 | import java.util.ArrayList; |
| 11 | import java.util.HashMap; | 12 | import java.util.HashMap; |
| 12 | 13 | ||
| @@ -14,11 +15,11 @@ import io.flutter.plugin.common.MethodCall; | @@ -14,11 +15,11 @@ import io.flutter.plugin.common.MethodCall; | ||
| 14 | import io.flutter.plugin.common.MethodChannel; | 15 | import io.flutter.plugin.common.MethodChannel; |
| 15 | 16 | ||
| 16 | public class PrintingHandler implements MethodChannel.MethodCallHandler { | 17 | public class PrintingHandler implements MethodChannel.MethodCallHandler { |
| 17 | - private final Activity activity; | 18 | + private final Context context; |
| 18 | private final MethodChannel channel; | 19 | private final MethodChannel channel; |
| 19 | 20 | ||
| 20 | - PrintingHandler(@NonNull Activity activity, @NonNull MethodChannel channel) { | ||
| 21 | - this.activity = activity; | 21 | + PrintingHandler(@NonNull Context context, @NonNull MethodChannel channel) { |
| 22 | + this.context = context; | ||
| 22 | this.channel = channel; | 23 | this.channel = channel; |
| 23 | } | 24 | } |
| 24 | 25 | ||
| @@ -32,7 +33,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -32,7 +33,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 32 | Double height = call.argument("height"); | 33 | Double height = call.argument("height"); |
| 33 | 34 | ||
| 34 | final PrintingJob printJob = | 35 | final PrintingJob printJob = |
| 35 | - new PrintingJob(activity, this, (int) call.argument("job")); | 36 | + new PrintingJob(context, this, (int) call.argument("job")); |
| 36 | assert name != null; | 37 | assert name != null; |
| 37 | printJob.printPdf(name, width, height); | 38 | printJob.printPdf(name, width, height); |
| 38 | 39 | ||
| @@ -41,7 +42,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -41,7 +42,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 41 | } | 42 | } |
| 42 | case "cancelJob": { | 43 | case "cancelJob": { |
| 43 | final PrintingJob printJob = | 44 | final PrintingJob printJob = |
| 44 | - new PrintingJob(activity, this, (int) call.argument("job")); | 45 | + new PrintingJob(context, this, (int) call.argument("job")); |
| 45 | printJob.cancelJob(null); | 46 | printJob.cancelJob(null); |
| 46 | result.success(1); | 47 | result.success(1); |
| 47 | break; | 48 | break; |
| @@ -52,7 +53,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -52,7 +53,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 52 | final String subject = call.argument("subject"); | 53 | final String subject = call.argument("subject"); |
| 53 | final String body = call.argument("body"); | 54 | final String body = call.argument("body"); |
| 54 | final ArrayList<String> emails = call.argument("emails"); | 55 | final ArrayList<String> emails = call.argument("emails"); |
| 55 | - PrintingJob.sharePdf(activity, document, name, subject, body, emails); | 56 | + PrintingJob.sharePdf(context, document, name, subject, body, emails); |
| 56 | result.success(1); | 57 | result.success(1); |
| 57 | break; | 58 | break; |
| 58 | } | 59 | } |
| @@ -64,7 +65,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -64,7 +65,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 64 | Double marginRight = call.argument("marginRight"); | 65 | Double marginRight = call.argument("marginRight"); |
| 65 | Double marginBottom = call.argument("marginBottom"); | 66 | Double marginBottom = call.argument("marginBottom"); |
| 66 | final PrintingJob printJob = | 67 | final PrintingJob printJob = |
| 67 | - new PrintingJob(activity, this, (int) call.argument("job")); | 68 | + new PrintingJob(context, this, (int) call.argument("job")); |
| 68 | 69 | ||
| 69 | assert width != null; | 70 | assert width != null; |
| 70 | assert height != null; | 71 | assert height != null; |
| @@ -98,7 +99,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -98,7 +99,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 98 | final ArrayList<Integer> pages = call.argument("pages"); | 99 | final ArrayList<Integer> pages = call.argument("pages"); |
| 99 | Double scale = call.argument("scale"); | 100 | Double scale = call.argument("scale"); |
| 100 | final PrintingJob printJob = | 101 | final PrintingJob printJob = |
| 101 | - new PrintingJob(activity, this, (int) call.argument("job")); | 102 | + new PrintingJob(context, this, (int) call.argument("job")); |
| 102 | printJob.rasterPdf(document, pages, scale); | 103 | printJob.rasterPdf(document, pages, scale); |
| 103 | result.success(1); | 104 | result.success(1); |
| 104 | break; | 105 | break; |
| @@ -16,10 +16,12 @@ | @@ -16,10 +16,12 @@ | ||
| 16 | 16 | ||
| 17 | package net.nfet.flutter.printing; | 17 | package net.nfet.flutter.printing; |
| 18 | 18 | ||
| 19 | -import android.app.Activity; | 19 | +import android.content.Context; |
| 20 | 20 | ||
| 21 | import androidx.annotation.NonNull; | 21 | import androidx.annotation.NonNull; |
| 22 | 22 | ||
| 23 | +import java.lang.ref.WeakReference; | ||
| 24 | + | ||
| 23 | import io.flutter.embedding.engine.plugins.FlutterPlugin; | 25 | import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 24 | import io.flutter.embedding.engine.plugins.activity.ActivityAware; | 26 | import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 25 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; | 27 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| @@ -30,20 +32,21 @@ import io.flutter.plugin.common.MethodChannel; | @@ -30,20 +32,21 @@ import io.flutter.plugin.common.MethodChannel; | ||
| 30 | * PrintingPlugin | 32 | * PrintingPlugin |
| 31 | */ | 33 | */ |
| 32 | public class PrintingPlugin implements FlutterPlugin, ActivityAware { | 34 | public class PrintingPlugin implements FlutterPlugin, ActivityAware { |
| 33 | - private Activity activity; | 35 | + private Context context; |
| 34 | private MethodChannel channel; | 36 | private MethodChannel channel; |
| 35 | private PrintingHandler handler; | 37 | private PrintingHandler handler; |
| 36 | 38 | ||
| 37 | @Override | 39 | @Override |
| 38 | public void onAttachedToEngine(FlutterPluginBinding binding) { | 40 | public void onAttachedToEngine(FlutterPluginBinding binding) { |
| 41 | + context = binding.getApplicationContext(); | ||
| 39 | onAttachedToEngine(binding.getBinaryMessenger()); | 42 | onAttachedToEngine(binding.getBinaryMessenger()); |
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | private void onAttachedToEngine(BinaryMessenger messenger) { | 45 | private void onAttachedToEngine(BinaryMessenger messenger) { |
| 43 | channel = new MethodChannel(messenger, "net.nfet.printing"); | 46 | channel = new MethodChannel(messenger, "net.nfet.printing"); |
| 44 | 47 | ||
| 45 | - if (activity != null) { | ||
| 46 | - handler = new PrintingHandler(activity, channel); | 48 | + if (context != null) { |
| 49 | + handler = new PrintingHandler(context, channel); | ||
| 47 | channel.setMethodCallHandler(handler); | 50 | channel.setMethodCallHandler(handler); |
| 48 | } | 51 | } |
| 49 | } | 52 | } |
| @@ -57,14 +60,16 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | @@ -57,14 +60,16 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | ||
| 57 | 60 | ||
| 58 | @Override | 61 | @Override |
| 59 | public void onAttachedToActivity(ActivityPluginBinding binding) { | 62 | public void onAttachedToActivity(ActivityPluginBinding binding) { |
| 60 | - onAttachedToActivity(binding.getActivity()); | 63 | + if (context != null) { |
| 64 | + context = null; | ||
| 65 | + } | ||
| 66 | + context = binding.getActivity(); | ||
| 67 | + onAttachedToActivity(context); | ||
| 61 | } | 68 | } |
| 62 | 69 | ||
| 63 | - private void onAttachedToActivity(Activity _activity) { | ||
| 64 | - activity = _activity; | ||
| 65 | - | ||
| 66 | - if (activity != null && channel != null) { | ||
| 67 | - handler = new PrintingHandler(activity, channel); | 70 | + private void onAttachedToActivity(Context context) { |
| 71 | + if (context != null && channel != null) { | ||
| 72 | + handler = new PrintingHandler(context, channel); | ||
| 68 | channel.setMethodCallHandler(handler); | 73 | channel.setMethodCallHandler(handler); |
| 69 | } | 74 | } |
| 70 | } | 75 | } |
| @@ -76,13 +81,15 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | @@ -76,13 +81,15 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | ||
| 76 | 81 | ||
| 77 | @Override | 82 | @Override |
| 78 | public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) { | 83 | public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) { |
| 79 | - onAttachedToActivity(binding.getActivity()); | 84 | + context = null; |
| 85 | + context = binding.getActivity(); | ||
| 86 | + onAttachedToActivity(context); | ||
| 80 | } | 87 | } |
| 81 | 88 | ||
| 82 | @Override | 89 | @Override |
| 83 | public void onDetachedFromActivity() { | 90 | public void onDetachedFromActivity() { |
| 84 | channel.setMethodCallHandler(null); | 91 | channel.setMethodCallHandler(null); |
| 85 | - activity = null; | 92 | + context = null; |
| 86 | handler = null; | 93 | handler = null; |
| 87 | } | 94 | } |
| 88 | } | 95 | } |
-
Please register or login to post a comment