Added PrintDialogActivity.java, and print_dialog.xml, implemented code to print qr codes via cloud print
svn path=/; revision=504
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<lint>
+ <issue id="JavascriptInterface" severity="ignore" />
+</lint>
\ No newline at end of file
android:layout_width="match_parent"
android:layout_height="match_parent" >
+ <Button
+ android:id="@+id/button1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignBaseline="@+id/button2"
+ android:layout_alignBottom="@+id/button2"
+ android:layout_alignParentLeft="true"
+ android:onClick="recognizeQRClick"
+ android:text="@string/recognize_qr_button" />
+
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="26dp"
- android:layout_marginLeft="24dp"
+ android:layout_alignParentRight="true"
+ android:layout_centerVertical="true"
android:ems="10"
- android:text="@string/hello_world" >
- </EditText>
+ android:text="Data 1" />
<EditText
- android:id="@+id/editText2"
+ android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button2"
android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_marginTop="154dp"
+ android:layout_below="@+id/editText1"
android:ems="10"
- android:hint="new data:" >
+ android:text="Data 2" />
- <requestFocus />
- </EditText>
+ <EditText
+ android:id="@+id/EditText02"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_below="@+id/EditText01"
+ android:ems="10"
+ android:text="Data 3" />
- <Button
- android:id="@+id/button2"
+ <EditText
+ android:id="@+id/EditText03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/editText2"
android:layout_alignParentRight="true"
- android:layout_marginBottom="66dp"
- android:onClick="scanQRClick"
- android:text="@string/scan_qr_button" />
+ android:layout_below="@+id/EditText02"
+ android:ems="10"
+ android:text="Data 4" />
- <Button
- android:id="@+id/button1"
+ <Print
+ android:id="@+id/print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/button2"
- android:layout_alignBottom="@+id/button2"
- android:layout_alignParentLeft="true"
- android:onClick="recognizeQRClick"
- android:text="@string/recognize_qr_button" />
+ android:layout_above="@+id/editText1"
+ android:layout_alignParentRight="true"
+ android:layout_marginBottom="80dp"
+ android:onClick="printQRClick"
+ android:text="@string/print_qr_button" />
+
</RelativeLayout>
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent">
+ <WebView android:id="@+id/webview"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"/>
+</RelativeLayout>
\ No newline at end of file
<string name="qr_button">Start QR code scanner</string>
<string name="inventory_button">Check inventory</string>
<string name="recognize_qr_button">Recognize QR</string>
- <string name="scan_qr_button">Scan in QR</string>
+ <string name="print_qr_button">Print new QR</string>
</resources>
\ No newline at end of file
package com.jaiswallab.lab.inventory;
-import com.google.zxing.integration.android.IntentIntegrator;
-import com.google.zxing.integration.android.IntentResult;
+import com.google.zxing.integration.android.*;
import android.app.Activity;
import android.content.Intent;
+import android.graphics.Bitmap;
+import android.net.Uri;
import android.os.Bundle;
+import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
- boolean scanningNewQR = false;
+ boolean printingNewQR = false;
boolean recognizingQR = false;
- private EditText text;
-
+ private EditText text1;
+ private EditText text2;
+ private EditText text3;
+ private EditText text4;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- text = (EditText) findViewById(R.id.editText1);
+ text1 = (EditText) findViewById(R.id.editText1);
}
@Override
// This method is called at click of the QR code button
public void recognizeQRClick(View view) {
- scanningNewQR = false;
+ printingNewQR = false;
recognizingQR = true;
- text.setText("Recognize QR button clicked");
+ text1.setText("Recognize QR button clicked");
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.initiateScan();
//startActivityForResult(intentIntegrator, 0);
}
+ // This method is called at click of the scan new QR button
+ public void printQRClick(View view) {
+ printingNewQR = true;
+ recognizingQR = false;
+ text1.setText("Print QR button clicked");
+ IntentIntegrator intentIntegrator = new IntentIntegrator(this);
+ intentIntegrator.initiateScan();
+ }
+
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String contents = scanResult.getContents();
- if(scanningNewQR) {
+ if(printingNewQR) {
addNewQR(contents);
} else if(recognizingQR){
displayQRInfo(contents);
}
- text.setText(contents);
+ text1.setText(contents);
// handle scan result
}
// else continue with any other code you need in the method
}
- // This method is called at click of the check inventory button
- public void scanQRClick(View view) {
- scanningNewQR = true;
- recognizingQR = false;
- text.setText("Scan QR button clicked");
- IntentIntegrator intentIntegrator = new IntentIntegrator(this);
- intentIntegrator.initiateScan();
- }
- //this will use the information passed to it to create a new ID in the databse
+
+ //this will use the information passed to it to create a new ID in the database
//it will also use any input from a variety of text edits in order to populate the database entry
//can we store pictures in the database if desired?
public void addNewQR(String id) {
return;
+ int rand1 = (int) (100*Math.random());
+ int rand2 = (int) (100*Math.random());
+ int rand3 = (int) (100*Math.random());
+ int rand4 = (int) (100*Math.random());
+ String barcode_content = text1.getText().toString() + Integer.toString(rand1)
+ + text2.getText().toString() + Integer.toString(rand2)
+ + text3.getText().toString() + Integer.toString(rand3)
+ + text4.getText().toString() + Integer.toString(rand4);
+ try {
+ // generate a 150x150 QR code
+ Bitmap barcode_bitmap = encodeAsBitmap(barcode_content, BarcodeFormat.QR_CODE, 150, 150);
+
+ String barcodeUrl = MediaStore.Images.Media.insertImage(getContentResolver(), barcode_bitmap, "NEW QR", "A newly created QR code");
+
+ Uri barcodeUri = Uri.parse(imgUrl);
+
+ Intent printIntent = new Intent(this, PrintDialogActivity.class);
+ printIntent.setDataAndType(barcodeUri, "image/bmp"); //TODO make sure image/bmp is correct.
+ printIntent.putExtra("title", "NEW QR");
+ startActivity(printIntent);
+
+ } catch (WriterException e) { //eek }
+
+ }
}
+
+
//this will use the information passed to it to reference an existing ID in the database
//it will display the database information in a useful format
public void displayQRInfo(String id) {
--- /dev/null
+package com.jaiswallab.lab.inventory;
+
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Base64;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class PrintDialogActivity extends Activity {
+ private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint/dialog.html";
+ private static final String JS_INTERFACE = "AndroidPrintDialog";
+ private static final String CONTENT_TRANSFER_ENCODING = "base64";
+
+ private static final String ZXING_URL = "http://zxing.appspot.com";
+ private static final int ZXING_SCAN_REQUEST = 65743;
+
+ /**
+ * Post message that is sent by Print Dialog web page when the printing dialog
+ * needs to be closed.
+ */
+ private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close";
+
+ /**
+ * Web view element to show the printing dialog in.
+ */
+ private WebView dialogWebView;
+
+ /**
+ * Intent that started the action.
+ */
+ Intent cloudPrintIntent;
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+
+ setContentView(R.layout.print_dialog);
+ dialogWebView = (WebView) findViewById(R.id.webview);
+ cloudPrintIntent = this.getIntent();
+
+ WebSettings settings = dialogWebView.getSettings();
+ settings.setJavaScriptEnabled(true);
+
+ dialogWebView.setWebViewClient(new PrintDialogWebClient());
+ dialogWebView.addJavascriptInterface(
+ new PrintDialogJavaScriptInterface(), JS_INTERFACE);
+
+ dialogWebView.loadUrl(PRINT_DIALOG_URL);
+ }
+
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+ if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) {
+ dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT"));
+ }
+ }
+
+ final class PrintDialogJavaScriptInterface {
+ public String getType() {
+ return cloudPrintIntent.getType();
+ }
+
+ public String getTitle() {
+ return cloudPrintIntent.getExtras().getString("title");
+ }
+
+ public String getContent() {
+ try {
+ ContentResolver contentResolver = getContentResolver();
+ InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ byte[] buffer = new byte[4096];
+ int n = is.read(buffer);
+ while (n >= 0) {
+ baos.write(buffer, 0, n);
+ n = is.read(buffer);
+ }
+ is.close();
+ baos.flush();
+
+ return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ public String getEncoding() {
+ return CONTENT_TRANSFER_ENCODING;
+ }
+
+ public void onPostMessage(String message) {
+ if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
+ finish();
+ }
+ }
+ }
+
+ private final class PrintDialogWebClient extends WebViewClient {
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ if (url.startsWith(ZXING_URL)) {
+ Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
+ intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
+ try {
+ startActivityForResult(intentScan, ZXING_SCAN_REQUEST);
+ } catch (ActivityNotFoundException error) {
+ view.loadUrl(url);
+ }
+ } else {
+ view.loadUrl(url);
+ }
+ return false;
+ }
+
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ if (PRINT_DIALOG_URL.equals(url)) {
+ // Submit print document.
+ view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument("
+ + "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle(),"
+ + "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))");
+
+ // Add post messages listener.
+ view.loadUrl("javascript:window.addEventListener('message',"
+ + "function(evt){window." + JS_INTERFACE + ".onPostMessage(evt.data)}, false)");
+ }
+ }
+ }
+}
\ No newline at end of file