Search in sources :

Example 1 with AddressedInterface

use of com.genonbeta.TrebleShot.util.AddressedInterface in project TrebleShot by genonbeta.

the class ShareActivity method doCommunicate.

protected void doCommunicate(final NetworkDeviceListAdapter.HotspotNetwork hotspotNetwork) {
    resetProgressItems();
    getProgressDialog().setMessage(getString(R.string.mesg_connectingToSelfHotspot));
    getProgressDialog().setMax(20);
    getProgressDialog().show();
    runOnWorkerService(new WorkerService.RunningTask(TAG, WORKER_TASK_CONNECT_TS_NETWORK) {

        private boolean mConnected = false;

        private boolean mConnectionToggled = false;

        private long mStartTime = System.currentTimeMillis();

        private String mRemoteAddress;

        @Override
        public void onRun() {
            while (mRemoteAddress == null) {
                int passedTime = (int) (System.currentTimeMillis() - mStartTime);
                if (!mDeviceListFragment.getWifiManager().isWifiEnabled()) {
                    if (!mDeviceListFragment.getWifiManager().setWifiEnabled(true))
                        // failed to start Wireless
                        break;
                } else if (!mDeviceListFragment.isConnectedToNetwork(hotspotNetwork) && !mConnectionToggled) {
                    mConnectionToggled = mDeviceListFragment.toggleConnection(hotspotNetwork);
                } else {
                    for (AddressedInterface addressedInterface : NetworkUtils.getInterfaces(true, null)) {
                        if (addressedInterface.getNetworkInterface().getDisplayName().startsWith(AppConfig.NETWORK_INTERFACE_WIFI)) {
                            String remoteAddress = NetworkUtils.getAddressPrefix(addressedInterface.getAssociatedAddress()) + "1";
                            if (NetworkUtils.ping(remoteAddress, 1000)) {
                                mRemoteAddress = remoteAddress;
                                break;
                            }
                        }
                    }
                }
                if (interruptionCheck(passedTime))
                    break;
            }
            if (mRemoteAddress != null) {
                try {
                    NetworkDeviceLoader.load(true, mDatabase, mRemoteAddress, new NetworkDeviceLoader.OnDeviceRegisteredErrorListener() {

                        @Override
                        public void onError(Exception error) {
                            getProgressDialog().dismiss();
                        }

                        @Override
                        public void onDeviceRegistered(AccessDatabase database, NetworkDevice device, final NetworkDevice.Connection connection) {
                            mConnected = true;
                            try {
                                hotspotNetwork.deviceId = device.deviceId;
                                mDatabase.reconstruct(hotspotNetwork);
                                device = hotspotNetwork;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            final NetworkDevice finalDevice = device;
                            if (!getDefaultInterrupter().interrupted())
                                runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        getProgressDialog().dismiss();
                                        doCommunicate(finalDevice, connection);
                                    }
                                });
                        }
                    });
                } catch (ConnectException e) {
                    e.printStackTrace();
                }
            }
            if (!mConnected) {
                getProgressDialog().dismiss();
                createSnackbar(R.string.mesg_connectionFailure).show();
            }
        // We can't add dialog outside of the else statement as it may close other dialogs as well
        }

        private boolean interruptionCheck(int passedTime) {
            try {
                Thread.sleep(1000);
                getProgressDialog().setProgress(passedTime / 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return true;
            } finally {
                if (passedTime > 20000 || getDefaultInterrupter().interrupted())
                    return true;
            }
            return false;
        }
    });
}
Also used : NetworkDevice(com.genonbeta.TrebleShot.object.NetworkDevice) ServiceConnection(android.content.ServiceConnection) WorkerService(com.genonbeta.TrebleShot.service.WorkerService) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) AddressedInterface(com.genonbeta.TrebleShot.util.AddressedInterface) AccessDatabase(com.genonbeta.TrebleShot.database.AccessDatabase) ConnectException(java.net.ConnectException)

Aggregations

ServiceConnection (android.content.ServiceConnection)1 AccessDatabase (com.genonbeta.TrebleShot.database.AccessDatabase)1 NetworkDevice (com.genonbeta.TrebleShot.object.NetworkDevice)1 WorkerService (com.genonbeta.TrebleShot.service.WorkerService)1 AddressedInterface (com.genonbeta.TrebleShot.util.AddressedInterface)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 JSONException (org.json.JSONException)1