Skip to content

Commit 228fb65

Browse files
committed
added clipboard
1 parent e0adf7f commit 228fb65

File tree

2 files changed

+77
-54
lines changed

2 files changed

+77
-54
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "ru.a402d.texttoprint"
77
minSdkVersion 19
88
targetSdkVersion 30
9-
versionCode 3
10-
versionName "1.1"
9+
versionCode 4
10+
versionName "1.2"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

app/src/main/java/ru/a402d/texttoprint/MainActivity.java

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.Manifest;
44
import android.annotation.SuppressLint;
5+
import android.content.ClipData;
6+
import android.content.ClipboardManager;
57
import android.content.Context;
68
import android.content.Intent;
79
import android.content.SharedPreferences;
@@ -35,10 +37,12 @@
3537
import java.nio.charset.StandardCharsets;
3638
import java.util.Locale;
3739
import java.util.Objects;
40+
3841
import androidx.appcompat.app.AppCompatActivity;
3942
import androidx.appcompat.widget.Toolbar;
43+
import androidx.core.view.MenuCompat;
4044

41-
public class MainActivity extends AppCompatActivity implements BillingProcessor.IBillingHandler {
45+
public class MainActivity extends AppCompatActivity implements BillingProcessor.IBillingHandler {
4246
private double fontSize = 17;
4347
private double printFontSize = 26;
4448
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 777;
@@ -58,19 +62,20 @@ public class MainActivity extends AppCompatActivity implements BillingProcessor.
5862
"}" +
5963
"</style>" +
6064
"</head>" +
61-
"<body>" ;
62-
String htmlBody;
63-
final String htmlFooter="</body></html>";
65+
"<body>";
66+
String htmlBody = "";
67+
final String htmlFooter = "</body></html>";
6468

6569
WebView webView;
70+
6671
@Override
6772
protected void onCreate(Bundle savedInstanceState) {
6873
super.onCreate(savedInstanceState);
6974
setContentView(R.layout.activity_main);
70-
Toolbar toolbar = findViewById(R.id.toolbar);
75+
Toolbar toolbar = findViewById(R.id.toolbar);
7176
setSupportActionBar(toolbar);
7277

73-
FloatingActionButton fab = findViewById(R.id.fab);
78+
FloatingActionButton fab = findViewById(R.id.fab);
7479
fab.setOnClickListener(new View.OnClickListener() {
7580
@Override
7681
public void onClick(View view) {
@@ -83,7 +88,7 @@ public void onClick(View view) {
8388
bp.initialize();
8489
}
8590

86-
webView = findViewById(R.id.wview);
91+
webView = findViewById(R.id.wview);
8792

8893
htmlBody = readAssets("html.html");
8994

@@ -94,7 +99,7 @@ public void onClick(View view) {
9499
}
95100

96101
// read html body from assets
97-
private String readAssets(@SuppressWarnings("SameParameterValue") String fileName){
102+
private String readAssets(@SuppressWarnings("SameParameterValue") String fileName) {
98103
try {
99104
StringBuilder buf = new StringBuilder();
100105
InputStream inputStream = getAssets().open(fileName);
@@ -107,18 +112,18 @@ private String readAssets(@SuppressWarnings("SameParameterValue") String fileNam
107112
}
108113
in.close();
109114
return buf.toString();
110-
}catch (Exception e){
115+
} catch (Exception e) {
111116
return "";
112117
}
113118

114119
}
115120

116-
private void parseIntent(Intent intent){
121+
private void parseIntent(Intent intent) {
117122

118123

119124
String action = intent.getAction();
120125
if (action == null || action.equals(Intent.ACTION_MAIN)) {
121-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+htmlBody+htmlFooter,"text/html; charset=utf-8","utf-8");
126+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8");
122127
return;
123128
}
124129
if (action.equals(Intent.ACTION_VIEW)) {
@@ -131,7 +136,8 @@ private void parseIntent(Intent intent){
131136
if (action.equals(Intent.ACTION_SEND)) {
132137
extras = intent.getExtras();
133138
if (extras == null) {
134-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+htmlBody+htmlFooter,"text/html; charset=utf-8","utf-8");
139+
htmlBody = "<h1>Error</h1><p>ACTION_SEND no Extras</p>";
140+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8");
135141
return;
136142
}
137143
Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
@@ -141,26 +147,26 @@ private void parseIntent(Intent intent){
141147
return;
142148
}
143149
if (stringText == null || stringText.trim().length() <= 0) {
144-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+htmlBody+htmlFooter,"text/html; charset=utf-8","utf-8");
150+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8");
145151
return;
146152
}
147153
stringBePrinted(stringText);
148154
}
149155

150156
}
151157

152-
private void stringBePrinted(String s){
158+
private void stringBePrinted(String s) {
153159
String[] separated = s.split("\n");
154-
StringBuilder ss= new StringBuilder();
155-
for (String v : separated ) {
160+
StringBuilder ss = new StringBuilder();
161+
for (String v : separated) {
156162
ss.append(v);
157163
ss.append("<br/>");
158164
}
159-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+ss.toString()+htmlFooter,"text/html; charset=utf-8","utf-8");
165+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + ss.toString() + htmlFooter, "text/html; charset=utf-8", "utf-8");
160166
// createWebPrintJob(webView);
161167
}
162168

163-
private void fileBePrinted(Uri uri){
169+
private void fileBePrinted(Uri uri) {
164170
BufferedReader br = null;
165171
StringBuilder sb = new StringBuilder();
166172
String line;
@@ -183,31 +189,33 @@ private void fileBePrinted(Uri uri){
183189
}
184190
}
185191

186-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+sb.toString()+htmlFooter,"text/html; charset=utf-8","utf-8");
192+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + sb.toString() + htmlFooter, "text/html; charset=utf-8", "utf-8");
187193
// createWebPrintJob(webView);
188194
}
189195

190196

191197
public boolean onCreateOptionsMenu(Menu menu) {
198+
MenuCompat.setGroupDividerEnabled(menu, true);
192199

193200
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
194-
menu.add(0,1,1,"Grant READ");
201+
menu.add(0, 1, 1, "Grant READ");
195202
}
196-
menu.add(0,2,2,"Font A");
197-
menu.add(0,3,3,"Font B");
198-
menu.add(0,4,4,"Font C");
199-
menu.add(0,5,5,"Font D");
200-
201-
menu.add(0,6,6,"Buy me a coffee");
203+
menu.add(1, 7, 2, "Demo text");
204+
menu.add(1, 9, 4, "Insert clipboard");
205+
menu.add(2, 2, 6, "Font A");
206+
menu.add(2, 3, 7, "Font B");
207+
menu.add(2, 4, 8, "Font C");
208+
menu.add(2, 5, 9, "Font D");
209+
menu.add(4, 6, 13, "Buy me a coffee");
202210
return super.onCreateOptionsMenu(menu);
203211
}
204212

205213
@Override
206214
public boolean onOptionsItemSelected(MenuItem item) {
207-
int id=item.getItemId();
208-
if(id==android.R.id.home){
215+
int id = item.getItemId();
216+
if (id == android.R.id.home) {
209217
finish();
210-
}else if(id==1){
218+
} else if (id == 1) {
211219
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
212220
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
213221
!= PackageManager.PERMISSION_GRANTED) {
@@ -217,38 +225,52 @@ public boolean onOptionsItemSelected(MenuItem item) {
217225

218226
}
219227
}
220-
}else if(id==2){
228+
} else if (id == 2) {
221229
saveFont(1);
222230
setFont(1);
223-
}else if(id==3){
231+
} else if (id == 3) {
224232
saveFont(2);
225233
setFont(2);
226-
}else if(id==4){
234+
} else if (id == 4) {
227235
saveFont(3);
228236
setFont(3);
229-
}else if(id==5){
237+
} else if (id == 5) {
230238
saveFont(4);
231239
setFont(4);
232-
}else if(id==6){
233-
if(bp!=null) {
240+
} else if (id == 6) {
241+
if (bp != null) {
234242
bp.purchase(this, "coffee");
235-
}else{
236-
Toast.makeText(this, R.string.no_bp,Toast.LENGTH_SHORT).show();
243+
} else {
244+
Toast.makeText(this, R.string.no_bp, Toast.LENGTH_SHORT).show();
237245
}
246+
} else if (id == 7) {
247+
htmlBody = readAssets("html.html");
248+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8");
249+
250+
} else if (id == 9) {
251+
try {
252+
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
253+
ClipData buf = Objects.requireNonNull(clipboard.getPrimaryClip());
254+
stringBePrinted(buf.getItemAt(0).coerceToText(this).toString());
255+
} catch (Exception e) {
256+
e.getStackTrace();
257+
}
258+
259+
238260
}
239261
return super.onOptionsItemSelected(item);
240262
}
241263

242264
@SuppressLint("ApplySharedPref")
243-
private void saveFont(int i){
265+
private void saveFont(int i) {
244266
sPref = getPreferences(MODE_PRIVATE);
245267
SharedPreferences.Editor ed = sPref.edit();
246268
ed.putInt(SAVED_SIZE, i);
247269
ed.commit();
248270
}
249271

250-
private void setFont(int s){
251-
switch (s){
272+
private void setFont(int s) {
273+
switch (s) {
252274
case 1:
253275
fontSize = 17;
254276
printFontSize = 26;
@@ -284,7 +306,7 @@ public void onRequestPermissionsResult(int requestCode,
284306

285307
@Override
286308
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
287-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+"<h1>Thanks !</h1>"+htmlFooter,"text/html; charset=utf-8","utf-8");
309+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + "<h1>Thanks !</h1>" + htmlFooter, "text/html; charset=utf-8", "utf-8");
288310
bp.consumePurchase(productId);
289311
}
290312

@@ -304,29 +326,30 @@ public void onBillingInitialized() {
304326
}
305327

306328

307-
public static class PrintDocumentAdapterWrapper extends PrintDocumentAdapter{
329+
public static class PrintDocumentAdapterWrapper extends PrintDocumentAdapter {
308330

309331
private final PrintDocumentAdapter delegate;
310-
PrintDocumentAdapterWrapper(PrintDocumentAdapter adapter){
332+
333+
PrintDocumentAdapterWrapper(PrintDocumentAdapter adapter) {
311334
super();
312335
this.delegate = adapter;
313336
}
314337

315338
@Override
316339
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
317-
delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras);
318-
Log.d("ANTSON","onLayout");
340+
delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras);
341+
Log.d("ANTSON", "onLayout");
319342
}
320343

321344
@Override
322345
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
323-
delegate.onWrite( pages, destination,cancellationSignal,callback);
324-
Log.d("ANTSON","onWrite");
346+
delegate.onWrite(pages, destination, cancellationSignal, callback);
347+
Log.d("ANTSON", "onWrite");
325348
}
326349

327-
public void onFinish(){
350+
public void onFinish() {
328351
delegate.onFinish();
329-
Log.d("ANTSON","onFinish");
352+
Log.d("ANTSON", "onFinish");
330353
}
331354

332355
}
@@ -345,9 +368,9 @@ private void createWebPrintJob(WebView webView) {
345368

346369
//open print dialog
347370
if (printManager != null) {
348-
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().setMinMargins(new PrintAttributes.Margins(0,0,0,0)).build());
349-
}else{
350-
webView.loadData(String.format(Locale.US,htmlHead,fontSize,printFontSize)+"PrintManager is null"+htmlFooter,"text/html; charset=utf-8","utf-8");
371+
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)).build());
372+
} else {
373+
webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + "PrintManager is null" + htmlFooter, "text/html; charset=utf-8", "utf-8");
351374

352375
}
353376
}

0 commit comments

Comments
 (0)