completed removing the rubber band effect

This commit is contained in:
alwin-iproat 2025-09-10 15:59:50 +05:30
parent 362b422db3
commit e24d33b64d

View File

@ -14,7 +14,6 @@ import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
@ -27,9 +26,6 @@ import androidx.appcompat.content.res.AppCompatResources;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import android.os.Process;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@ -44,49 +40,19 @@ public class MainActivity extends AppCompatActivity {
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (granted && device != null) {
Log.d("USB", "Permission granted for: " + device);
usbDevice = device;
if (!setupPrinterConnection(device)) {
Log.d("USB", "Permission ok, but no interface/endpoint found.");
if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
synchronized (this) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
setupPrinterConnection(device);
}
}
} else {
Log.d("USB", "Permission denied for device: " + device);
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
Log.d("USB", "Device attached: " + device);
usbDevice = device;
PendingIntent permissionIntent = PendingIntent.getBroadcast(
context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
);
usbManager.requestPermission(device, permissionIntent);
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null && device.equals(usbDevice)) {
Log.d("USB", "Device detached: " + device);
usbDevice = null;
usbInterface = null;
usbEndpoint = null;
usbDeviceConnection = null;
}
}
}
};
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
@ -102,33 +68,18 @@ public class MainActivity extends AppCompatActivity {
//
// webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
// webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
//// webView.loadUrl("file:///android_asset/index.html");
//// webView.loadUrl("file:///android_asset/index.html");
// findUsbPrinter();
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(usbReceiver, filter, RECEIVER_NOT_EXPORTED);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
WebView webView = new WebView(this);
setContentView(webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.setWebViewClient(new WebViewClient());
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
<<<<<<< HEAD
WebView webView = new WebView(this);
setContentView(webView);
@ -155,146 +106,77 @@ public class MainActivity extends AppCompatActivity {
//webView.loadUrl("file:///android_asset/index.html");
findUsbPrinter();
}
=======
webView.loadUrl("http://10.23.107.10:8000/kioskvideo");
// webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
//webView.loadUrl("file:///android_asset/index.html");
findUsbPrinter();
}
>>>>>>> a2180dfaea0d2de73bced46b7e6e0fb48b068539
private void findUsbPrinter() {
private void findUsbPrinter(){
// Find the first connected USB device
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
boolean looksLikePrinter = false;
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface intf = device.getInterface(i);
// Prefer PRINTER or VENDOR_SPEC, but accept any interface that has BULK OUT
if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER
|| intf.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC) {
looksLikePrinter = true;
Integer intfClass = intf.getInterfaceClass();
if (intfClass == UsbConstants.USB_CLASS_PRINTER || intfClass == UsbConstants.USB_CLASS_VENDOR_SPEC) {
usbInterface = intf;
usbDevice = device;
PendingIntent permissionIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
);
if (!usbManager.hasPermission(usbDevice)) {
usbManager.requestPermission(usbDevice, permissionIntent);
} else {
setupPrinterConnection(usbDevice);
}
break;
}
for (int j = 0; j < intf.getEndpointCount(); j++) {
UsbEndpoint ep = intf.getEndpoint(j);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
&& ep.getDirection() == UsbConstants.USB_DIR_OUT) {
looksLikePrinter = true;
break;
}
}
if (looksLikePrinter) break;
}
if (looksLikePrinter) {
usbDevice = device;
PendingIntent permissionIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
);
if (!usbManager.hasPermission(device)) {
usbManager.requestPermission(device, permissionIntent);
} else {
// We already have permission; now build interface+endpoint
if (!setupPrinterConnection(device)) {
Log.d("USB", "findUsbPrinter: permission ok but no interface/endpoint");
}
}
return; // stop after first suitable device
}
}
Log.d("USB", "findUsbPrinter: No suitable USB printer device found");
}
private boolean setupPrinterConnection(UsbDevice device) {
usbInterface = null;
usbEndpoint = null;
if (device == null) {
Log.d("USB", "setupPrinterConnection: device is null");
return false;
}
// Pick the first interface that has a BULK OUT endpoint
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface intf = device.getInterface(i);
for (int j = 0; j < intf.getEndpointCount(); j++) {
UsbEndpoint ep = intf.getEndpoint(j);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
&& ep.getDirection() == UsbConstants.USB_DIR_OUT) {
usbInterface = intf;
usbEndpoint = ep;
Log.d("USB", "setupPrinterConnection: interface#" + i + ", OUT endpoint#" + j + " set");
return true;
}
private void setupPrinterConnection(UsbDevice device){
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
UsbEndpoint ep = usbInterface.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK &&
ep.getDirection() == UsbConstants.USB_DIR_OUT) {
usbEndpoint = ep;
break;
}
}
Log.d("USB", "setupPrinterConnection: No printer interface or BULK OUT endpoint found");
return false;
}
private void print(String text) throws Exception {
if (usbDevice == null) {
Log.d("USB", "print: usbDevice is null; trying discovery");
findUsbPrinter();
if (usbDevice == null) {
System.out.println("No USB device found.");
return;
}
if (usbInterface == null) {
System.out.println("No printer interface found.");
return;
}
// Ensure we have a valid interface/endpoint for the current device
if (usbInterface == null || usbEndpoint == null) {
if (!setupPrinterConnection(usbDevice)) {
System.out.println("No printer interface or BULK OUT endpoint found.");
return;
}
if (usbEndpoint == null) {
System.out.println("No bulk OUT endpoint found.");
return;
}
usbDeviceConnection = usbManager.openDevice(usbDevice);
if (usbDeviceConnection == null) {
System.out.println("Unable to open connection. Requesting permission again...");
PendingIntent permissionIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
);
usbManager.requestPermission(usbDevice, permissionIntent);
System.out.println("Unable to open connection.");
return;
}
boolean claimed = usbDeviceConnection.claimInterface(usbInterface, true);
if (!claimed) {
System.out.println("Failed to claim interface.");
usbDeviceConnection.close();
usbDeviceConnection.claimInterface(usbInterface, true);
if(usbDeviceConnection == null){
return;
}
try {
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
printer.printFormatted(text);
printer.feed(5);
printer.cut();
} finally {
usbDeviceConnection.releaseInterface(usbInterface);
usbDeviceConnection.close();
}
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
printer.printFormatted(text);
printer.feed(5);
printer.cut();
usbDeviceConnection.releaseInterface(usbInterface);
usbDeviceConnection.close();
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c){