mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-11-04 04:17:46 +00:00 
			
		
		
		
	refactor duplicated code in close method
This commit is contained in:
		
							parent
							
								
									5767298636
								
							
						
					
					
						commit
						24187b3af6
					
				@ -239,26 +239,11 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
        public void close() throws IOException {
 | 
					        public void closeInt() {
 | 
				
			||||||
            if (mConnection == null) {
 | 
					 | 
				
			||||||
                throw new IOException("Already closed");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            synchronized (this) {
 | 
					 | 
				
			||||||
                if (mUsbRequest != null)
 | 
					 | 
				
			||||||
                    mUsbRequest.cancel();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            mControlEndpoint = null;
 | 
					 | 
				
			||||||
            mReadEndpoint = null;
 | 
					 | 
				
			||||||
            mWriteEndpoint = null;
 | 
					 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                mConnection.releaseInterface(mControlInterface);
 | 
					                mConnection.releaseInterface(mControlInterface);
 | 
				
			||||||
                mConnection.releaseInterface(mDataInterface);
 | 
					                mConnection.releaseInterface(mDataInterface);
 | 
				
			||||||
            } catch(Exception ignored) {}
 | 
					            } catch(Exception ignored) {}
 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                mConnection.close();
 | 
					 | 
				
			||||||
            } finally {
 | 
					 | 
				
			||||||
                mConnection = null;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
 | 
				
			|||||||
@ -129,23 +129,11 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Override
 | 
							@Override
 | 
				
			||||||
		public void close() throws IOException {
 | 
							public void closeInt() {
 | 
				
			||||||
			if (mConnection == null) {
 | 
					 | 
				
			||||||
				throw new IOException("Already closed");
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			synchronized (this) {
 | 
					 | 
				
			||||||
				if (mUsbRequest != null)
 | 
					 | 
				
			||||||
					mUsbRequest.cancel();
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			try {
 | 
								try {
 | 
				
			||||||
				for (int i = 0; i < mDevice.getInterfaceCount(); i++)
 | 
									for (int i = 0; i < mDevice.getInterfaceCount(); i++)
 | 
				
			||||||
					mConnection.releaseInterface(mDevice.getInterface(i));
 | 
										mConnection.releaseInterface(mDevice.getInterface(i));
 | 
				
			||||||
			} catch(Exception ignored) {}
 | 
								} catch(Exception ignored) {}
 | 
				
			||||||
			try {
 | 
					 | 
				
			||||||
				mConnection.close();
 | 
					 | 
				
			||||||
			} finally {
 | 
					 | 
				
			||||||
				mConnection = null;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private int controlOut(int request, int value, int index) {
 | 
							private int controlOut(int request, int value, int index) {
 | 
				
			||||||
 | 
				
			|||||||
@ -109,7 +109,26 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
 | 
				
			|||||||
    public abstract void open(UsbDeviceConnection connection) throws IOException;
 | 
					    public abstract void open(UsbDeviceConnection connection) throws IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public abstract void close() throws IOException;
 | 
					    public void close() throws IOException {
 | 
				
			||||||
 | 
					        if (mConnection == null) {
 | 
				
			||||||
 | 
					            throw new IOException("Already closed");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        synchronized (this) {
 | 
				
			||||||
 | 
					            if (mUsbRequest != null)
 | 
				
			||||||
 | 
					                mUsbRequest.cancel();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            closeInt();
 | 
				
			||||||
 | 
					        } catch(Exception ignored) {}
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            mConnection.close();
 | 
				
			||||||
 | 
					        } finally {
 | 
				
			||||||
 | 
					            mConnection = null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected abstract void closeInt();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public int read(final byte[] dest, final int timeoutMillis) throws IOException {
 | 
					    public int read(final byte[] dest, final int timeoutMillis) throws IOException {
 | 
				
			||||||
 | 
				
			|||||||
@ -166,26 +166,13 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
        public void close() throws IOException {
 | 
					        public void closeInt() {
 | 
				
			||||||
            if (mConnection == null) {
 | 
					 | 
				
			||||||
                throw new IOException("Already closed");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            synchronized (this) {
 | 
					 | 
				
			||||||
                if(mUsbRequest != null) {
 | 
					 | 
				
			||||||
                    mUsbRequest.cancel();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
 | 
					                setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
 | 
				
			||||||
            } catch (Exception ignored) {}
 | 
					            } catch (Exception ignored) {}
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
 | 
					                mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
 | 
				
			||||||
            } catch(Exception ignored) {}
 | 
					            } catch(Exception ignored) {}
 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                mConnection.close();
 | 
					 | 
				
			||||||
            } finally {
 | 
					 | 
				
			||||||
                mConnection = null;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void setBaudRate(int baudRate) throws IOException {
 | 
					        private void setBaudRate(int baudRate) throws IOException {
 | 
				
			||||||
 | 
				
			|||||||
@ -266,18 +266,10 @@ public class FtdiSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
        public void close() throws IOException {
 | 
					        public void closeInt() {
 | 
				
			||||||
            if (mConnection == null) {
 | 
					 | 
				
			||||||
                throw new IOException("Already closed");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
 | 
					                mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
 | 
				
			||||||
            } catch(Exception ignored) {}
 | 
					            } catch(Exception ignored) {}
 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                mConnection.close();
 | 
					 | 
				
			||||||
            } finally {
 | 
					 | 
				
			||||||
                mConnection = null;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private int setBaudRate(int baudRate) throws IOException {
 | 
					        private int setBaudRate(int baudRate) throws IOException {
 | 
				
			||||||
 | 
				
			|||||||
@ -338,10 +338,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
        public void close() throws IOException {
 | 
					        public void closeInt() {
 | 
				
			||||||
            if (mConnection == null) {
 | 
					 | 
				
			||||||
                throw new IOException("Already closed");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                mStopReadStatusThread = true;
 | 
					                mStopReadStatusThread = true;
 | 
				
			||||||
                synchronized (mReadStatusThreadLock) {
 | 
					                synchronized (mReadStatusThreadLock) {
 | 
				
			||||||
@ -354,16 +351,10 @@ public class ProlificSerialDriver implements UsbSerialDriver {
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                resetDevice();
 | 
					                resetDevice();
 | 
				
			||||||
            } finally {
 | 
					            } catch(Exception ignored) {}
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                mConnection.releaseInterface(mDevice.getInterface(0));
 | 
					                mConnection.releaseInterface(mDevice.getInterface(0));
 | 
				
			||||||
            } catch(Exception ignored) {}
 | 
					            } catch(Exception ignored) {}
 | 
				
			||||||
                try {
 | 
					 | 
				
			||||||
                    mConnection.close();
 | 
					 | 
				
			||||||
                } finally {
 | 
					 | 
				
			||||||
                    mConnection = null;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @Override
 | 
					        @Override
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user