Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David PHAM-VAN
2024-01-31 08:57:14 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
fb29442f5e85404d8759573326da6d1801fac320
fb29442f
1 parent
adf4ff1d
Android lint
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
115 deletions
printing/android/src/main/java/net/nfet/flutter/printing/PrintingHandler.java
printing/android/src/main/java/net/nfet/flutter/printing/PrintingJob.java
printing/android/src/main/java/net/nfet/flutter/printing/PrintingPlugin.java
printing/android/src/main/java/net/nfet/flutter/printing/PrintingHandler.java
View file @
fb29442
...
...
@@ -7,7 +7,6 @@ import android.print.PrintAttributes;
import
androidx.annotation.NonNull
;
import
androidx.annotation.RequiresApi
;
import
java.lang.ref.WeakReference
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
...
...
@@ -35,6 +34,8 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler {
final
PrintingJob
printJob
=
new
PrintingJob
(
context
,
this
,
(
int
)
call
.
argument
(
"job"
));
assert
name
!=
null
;
assert
width
!=
null
;
assert
height
!=
null
;
printJob
.
printPdf
(
name
,
width
,
height
);
result
.
success
(
1
);
...
...
@@ -138,7 +139,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler {
}
@Override
public
void
error
(
String
errorCode
,
String
errorMessage
,
Object
errorDetails
)
{
public
void
error
(
@NonNull
String
errorCode
,
String
errorMessage
,
Object
errorDetails
)
{
printJob
.
cancelJob
(
errorMessage
);
}
...
...
printing/android/src/main/java/net/nfet/flutter/printing/PrintingJob.java
View file @
fb29442
...
...
@@ -137,54 +137,44 @@ public class PrintingJob extends PrintDocumentAdapter {
@Override
public
void
onFinish
()
{
Thread
thread
=
new
Thread
(
new
Runnable
()
{
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
LOLLIPOP
)
@Override
public
void
run
()
{
try
{
final
boolean
[]
wait
=
{
true
};
int
count
=
5
*
60
*
10
;
// That's 10 minutes.
while
(
wait
[
0
])
{
new
Handler
(
Looper
.
getMainLooper
()).
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
int
state
=
printJob
==
null
?
PrintJobInfo
.
STATE_FAILED
:
printJob
.
getInfo
().
getState
();
if
(
state
==
PrintJobInfo
.
STATE_COMPLETED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
true
,
null
);
wait
[
0
]
=
false
;
}
else
if
(
state
==
PrintJobInfo
.
STATE_CANCELED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
false
,
null
);
wait
[
0
]
=
false
;
}
else
if
(
state
==
PrintJobInfo
.
STATE_FAILED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
false
,
"Unable to print"
);
wait
[
0
]
=
false
;
}
}
});
if
(--
count
<=
0
)
{
throw
new
Exception
(
"Timeout waiting for the job to finish"
);
Thread
thread
=
new
Thread
(()
->
{
try
{
final
boolean
[]
wait
=
{
true
};
int
count
=
5
*
60
*
10
;
// That's 10 minutes.
while
(
wait
[
0
])
{
new
Handler
(
Looper
.
getMainLooper
()).
post
(()
->
{
int
state
=
printJob
==
null
?
PrintJobInfo
.
STATE_FAILED
:
printJob
.
getInfo
().
getState
();
if
(
state
==
PrintJobInfo
.
STATE_COMPLETED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
true
,
null
);
wait
[
0
]
=
false
;
}
else
if
(
state
==
PrintJobInfo
.
STATE_CANCELED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
false
,
null
);
wait
[
0
]
=
false
;
}
else
if
(
state
==
PrintJobInfo
.
STATE_FAILED
)
{
printing
.
onCompleted
(
PrintingJob
.
this
,
false
,
"Unable to print"
);
wait
[
0
]
=
false
;
}
});
if
(
wait
[
0
])
{
Thread
.
sleep
(
200
);
}
if
(--
count
<=
0
)
{
throw
new
Exception
(
"Timeout waiting for the job to finish"
);
}
}
catch
(
final
Exception
e
)
{
new
Handler
(
Looper
.
getMainLooper
()).
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
printing
.
onCompleted
(
PrintingJob
.
this
,
printJob
!=
null
&&
printJob
.
isCompleted
(),
e
.
getMessage
());
}
});
}
printJob
=
null
;
if
(
wait
[
0
])
{
Thread
.
sleep
(
200
);
}
}
}
catch
(
final
Exception
e
)
{
new
Handler
(
Looper
.
getMainLooper
())
.
post
(()
->
printing
.
onCompleted
(
PrintingJob
.
this
,
printJob
!=
null
&&
printJob
.
isCompleted
(),
e
.
getMessage
()));
}
printJob
=
null
;
});
thread
.
start
();
...
...
@@ -439,86 +429,67 @@ public class PrintingJob extends PrintDocumentAdapter {
return
;
}
Thread
thread
=
new
Thread
(
new
Runnable
()
{
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
LOLLIPOP
)
@Override
public
void
run
()
{
String
error
=
null
;
try
{
File
tempDir
=
context
.
getCacheDir
();
File
file
=
File
.
createTempFile
(
"printing"
,
null
,
tempDir
);
FileOutputStream
oStream
=
new
FileOutputStream
(
file
);
oStream
.
write
(
data
);
oStream
.
close
();
FileInputStream
iStream
=
new
FileInputStream
(
file
);
ParcelFileDescriptor
parcelFD
=
ParcelFileDescriptor
.
dup
(
iStream
.
getFD
());
PdfRenderer
renderer
=
new
PdfRenderer
(
parcelFD
);
if
(!
file
.
delete
())
{
Log
.
e
(
"PDF"
,
"Unable to delete temporary file"
);
}
final
int
pageCount
=
pages
!=
null
?
pages
.
size
()
:
renderer
.
getPageCount
();
for
(
int
i
=
0
;
i
<
pageCount
;
i
++)
{
PdfRenderer
.
Page
page
=
renderer
.
openPage
(
pages
==
null
?
i
:
pages
.
get
(
i
));
final
int
width
=
Double
.
valueOf
(
page
.
getWidth
()
*
scale
).
intValue
();
final
int
height
=
Double
.
valueOf
(
page
.
getHeight
()
*
scale
).
intValue
();
int
stride
=
width
*
4
;
Thread
thread
=
new
Thread
(()
->
{
String
error
=
null
;
try
{
File
tempDir
=
context
.
getCacheDir
();
File
file
=
File
.
createTempFile
(
"printing"
,
null
,
tempDir
);
FileOutputStream
oStream
=
new
FileOutputStream
(
file
);
oStream
.
write
(
data
);
oStream
.
close
();
FileInputStream
iStream
=
new
FileInputStream
(
file
);
ParcelFileDescriptor
parcelFD
=
ParcelFileDescriptor
.
dup
(
iStream
.
getFD
());
PdfRenderer
renderer
=
new
PdfRenderer
(
parcelFD
);
if
(!
file
.
delete
())
{
Log
.
e
(
"PDF"
,
"Unable to delete temporary file"
);
}
Matrix
transform
=
new
Matrix
();
transform
.
setScale
(
scale
.
floatValue
(),
scale
.
floatValue
());
final
int
pageCount
=
pages
!=
null
?
pages
.
size
()
:
renderer
.
getPageCount
();
for
(
int
i
=
0
;
i
<
pageCount
;
i
++)
{
PdfRenderer
.
Page
page
=
renderer
.
openPage
(
pages
==
null
?
i
:
pages
.
get
(
i
));
Bitmap
bitmap
=
Bitmap
.
createBitmap
(
width
,
height
,
Bitmap
.
Config
.
ARGB_8888
);
final
int
width
=
Double
.
valueOf
(
page
.
getWidth
()
*
scale
).
intValue
();
final
int
height
=
Double
.
valueOf
(
page
.
getHeight
()
*
scale
).
intValue
();
int
stride
=
width
*
4
;
page
.
render
(
bitmap
,
null
,
transform
,
PdfRenderer
.
Page
.
RENDER_MODE_FOR_DISPLAY
);
Matrix
transform
=
new
Matrix
();
transform
.
setScale
(
scale
.
floatValue
(),
scale
.
floatValue
());
page
.
close
(
);
Bitmap
bitmap
=
Bitmap
.
createBitmap
(
width
,
height
,
Bitmap
.
Config
.
ARGB_8888
);
final
ByteBuffer
buf
=
ByteBuffer
.
allocate
(
stride
*
height
);
bitmap
.
copyPixelsToBuffer
(
buf
);
bitmap
.
recycle
();
page
.
render
(
bitmap
,
null
,
transform
,
PdfRenderer
.
Page
.
RENDER_MODE_FOR_DISPLAY
);
new
Handler
(
Looper
.
getMainLooper
()).
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
printing
.
onPageRasterized
(
PrintingJob
.
this
,
buf
.
array
(),
width
,
height
);
}
});
}
page
.
close
();
renderer
.
close
();
iStream
.
close
();
final
ByteBuffer
buf
=
ByteBuffer
.
allocate
(
stride
*
height
);
bitmap
.
copyPixelsToBuffer
(
buf
);
bitmap
.
recycle
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
error
=
e
.
getMessage
();
new
Handler
(
Looper
.
getMainLooper
())
.
post
(()
->
printing
.
onPageRasterized
(
PrintingJob
.
this
,
buf
.
array
(),
width
,
height
));
}
final
String
finalError
=
error
;
new
Handler
(
Looper
.
getMainLooper
()).
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
printing
.
onPageRasterEnd
(
PrintingJob
.
this
,
finalError
);
}
});
renderer
.
close
();
iStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
error
=
e
.
getMessage
();
}
final
String
finalError
=
error
;
new
Handler
(
Looper
.
getMainLooper
())
.
post
(()
->
printing
.
onPageRasterEnd
(
PrintingJob
.
this
,
finalError
));
});
thread
.
setUncaughtExceptionHandler
(
new
Thread
.
UncaughtExceptionHandler
()
{
@Override
public
void
uncaughtException
(
Thread
t
,
Throwable
e
)
{
final
String
finalError
=
e
.
getMessage
();
new
Handler
(
Looper
.
getMainLooper
()).
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
printing
.
onPageRasterEnd
(
PrintingJob
.
this
,
finalError
);
}
});
}
thread
.
setUncaughtExceptionHandler
((
t
,
e
)
->
{
final
String
finalError
=
e
.
getMessage
();
new
Handler
(
Looper
.
getMainLooper
())
.
post
(()
->
printing
.
onPageRasterEnd
(
PrintingJob
.
this
,
finalError
));
});
thread
.
start
();
...
...
printing/android/src/main/java/net/nfet/flutter/printing/PrintingPlugin.java
View file @
fb29442
...
...
@@ -20,8 +20,6 @@ import android.content.Context;
import
androidx.annotation.NonNull
;
import
java.lang.ref.WeakReference
;
import
io.flutter.embedding.engine.plugins.FlutterPlugin
;
import
io.flutter.embedding.engine.plugins.activity.ActivityAware
;
import
io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
;
...
...
@@ -59,7 +57,7 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware {
}
@Override
public
void
onAttachedToActivity
(
ActivityPluginBinding
binding
)
{
public
void
onAttachedToActivity
(
@NonNull
ActivityPluginBinding
binding
)
{
if
(
context
!=
null
)
{
context
=
null
;
}
...
...
Please
register
or
login
to post a comment