use of com.genonbeta.TrebleShot.object.NetworkDevice in project TrebleShot by genonbeta.
the class ShareActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
if (getSupportActionBar() != null)
getSupportActionBar().setTitle(R.string.text_shareWithTrebleshot);
mFAB = findViewById(R.id.content_fab);
mDatabase = new AccessDatabase(getApplicationContext());
mDeviceListFragment = (NetworkDeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.activity_share_fragment);
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mDeviceListFragment.getListView().setPadding(0, 0, 0, 300);
mDeviceListFragment.getListView().setClipToPadding(false);
mFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fabClicked();
}
});
mDeviceListFragment.setOnListClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NetworkDevice device = (NetworkDevice) mDeviceListFragment.getListAdapter().getItem(position);
if (device instanceof NetworkDeviceListAdapter.HotspotNetwork)
doCommunicate((NetworkDeviceListAdapter.HotspotNetwork) device);
else
showChooserDialog(device);
}
});
bindService(new Intent(this, WorkerService.class), mWorkerConnection, Context.BIND_AUTO_CREATE);
}
use of com.genonbeta.TrebleShot.object.NetworkDevice 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;
}
});
}
use of com.genonbeta.TrebleShot.object.NetworkDevice in project TrebleShot by genonbeta.
the class HomeActivity method onStart.
@Override
protected void onStart() {
super.onStart();
mIsStopped = false;
View headerView = mNavigationView.getHeaderView(0);
if (headerView != null) {
NetworkDevice localDevice = AppUtils.getLocalDevice(getApplicationContext());
ImageView imageView = headerView.findViewById(R.id.header_default_device_image);
TextView deviceNameText = headerView.findViewById(R.id.header_default_device_name_text);
TextView versionText = headerView.findViewById(R.id.header_default_device_version_text);
String firstLetters = TextUtils.getFirstLetters(localDevice.nickname, 1);
TextDrawable drawable = TextDrawable.builder().buildRoundRect(firstLetters.length() > 0 ? firstLetters : "?", ContextCompat.getColor(getApplicationContext(), R.color.networkDeviceRipple), 100);
imageView.setImageDrawable(drawable);
deviceNameText.setText(localDevice.nickname);
versionText.setText(localDevice.versionName);
}
}
use of com.genonbeta.TrebleShot.object.NetworkDevice in project TrebleShot by genonbeta.
the class ShareActivity method showChooserDialog.
protected void showChooserDialog(final NetworkDevice device) {
device.isRestricted = false;
mDatabase.publish(device);
new ConnectionChooserDialog(ShareActivity.this, mDatabase, device, new ConnectionChooserDialog.OnDeviceSelectedListener() {
@Override
public void onDeviceSelected(final NetworkDevice.Connection connection, ArrayList<NetworkDevice.Connection> availableInterfaces) {
doCommunicate(device, connection);
}
}, true).show();
}
use of com.genonbeta.TrebleShot.object.NetworkDevice in project TrebleShot by genonbeta.
the class NetworkDeviceLoader method load.
public static NetworkDevice load(boolean currentThread, final AccessDatabase database, final String ipAddress, final OnDeviceRegisteredListener listener) throws ConnectException {
CommunicationBridge.Client.ConnectionHandler connectionHandler = new CommunicationBridge.Client.ConnectionHandler() {
@Override
public void onConnect(CommunicationBridge.Client client) {
try {
NetworkDevice device = client.loadDevice(ipAddress);
if (device.deviceId != null) {
NetworkDevice localDevice = AppUtils.getLocalDevice(database.getContext());
NetworkDevice.Connection connection = processConnection(database, device, ipAddress);
if (!localDevice.deviceId.equals(device.deviceId)) {
device.lastUsageTime = System.currentTimeMillis();
database.publish(device);
if (listener != null)
listener.onDeviceRegistered(database, device, connection);
}
}
client.setReturn(device);
} catch (Exception e) {
if (listener != null && listener instanceof OnDeviceRegisteredErrorListener)
((OnDeviceRegisteredErrorListener) listener).onError(e);
}
}
};
if (currentThread)
return CommunicationBridge.connect(database, NetworkDevice.class, connectionHandler);
else
CommunicationBridge.connect(database, connectionHandler);
return null;
}
Aggregations