2015-07-28 17:30:20 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
2015-04-06 20:58:35 +02:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2015-04-20 20:49:14 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2015-04-06 20:58:35 +02:00
|
|
|
import android.content.Intent;
|
2015-04-20 20:49:14 +02:00
|
|
|
import android.content.IntentFilter;
|
2015-04-06 20:58:35 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.NavUtils;
|
2015-04-20 20:49:14 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-04-06 20:58:35 +02:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
2015-08-05 17:31:11 +02:00
|
|
|
import android.widget.Toast;
|
2015-04-06 20:58:35 +02:00
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-06 21:35:00 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-08-06 02:17:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-06 21:35:00 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
2015-08-06 02:17:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-08-05 17:31:11 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2015-04-06 20:58:35 +02:00
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
public class FwAppInstallerActivity extends Activity implements InstallActivity {
|
2015-04-06 20:58:35 +02:00
|
|
|
|
2015-07-28 17:30:20 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(FwAppInstallerActivity.class);
|
2015-04-06 20:58:35 +02:00
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
private TextView fwAppInstallTextView;
|
|
|
|
private Button installButton;
|
|
|
|
private Uri uri;
|
|
|
|
private GBDevice device;
|
|
|
|
private InstallHandler installHandler;
|
|
|
|
private boolean mayConnect;
|
2015-07-28 17:30:20 +02:00
|
|
|
|
2015-04-20 20:49:14 +02:00
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (action.equals(ControlCenter.ACTION_QUIT)) {
|
|
|
|
finish();
|
|
|
|
} else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
2015-08-06 02:17:38 +02:00
|
|
|
device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
if (device != null) {
|
|
|
|
if (!device.isConnected()) {
|
2015-08-14 23:37:47 +02:00
|
|
|
setInstallEnabled(false);
|
2015-08-06 02:17:38 +02:00
|
|
|
if (mayConnect) {
|
|
|
|
GB.toast(FwAppInstallerActivity.this, getString(R.string.connecting), Toast.LENGTH_SHORT, GB.INFO);
|
|
|
|
connect();
|
2015-04-20 20:49:14 +02:00
|
|
|
} else {
|
2015-08-06 21:35:00 +02:00
|
|
|
setInfoText(device.getStateString());
|
2015-04-20 20:49:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-06 02:17:38 +02:00
|
|
|
validateInstallation();
|
2015-04-20 20:49:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
private void connect() {
|
2015-08-06 21:35:00 +02:00
|
|
|
mayConnect = false; // only do that once per #onCreate
|
2015-08-06 02:17:38 +02:00
|
|
|
Intent startIntent = new Intent(FwAppInstallerActivity.this, DeviceCommunicationService.class);
|
|
|
|
startIntent.setAction(DeviceCommunicationService.ACTION_CONNECT);
|
2015-08-06 21:35:00 +02:00
|
|
|
if (device != null) {
|
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
|
|
|
|
}
|
2015-08-06 02:17:38 +02:00
|
|
|
startService(startIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateInstallation() {
|
|
|
|
if (installHandler != null) {
|
|
|
|
installHandler.validateInstallation(this, device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 20:58:35 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_appinstaller);
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
2015-08-06 21:35:00 +02:00
|
|
|
GBDevice dev = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
if (dev != null) {
|
|
|
|
device = dev;
|
|
|
|
}
|
2015-08-06 02:17:38 +02:00
|
|
|
mayConnect = true;
|
2015-07-28 17:30:20 +02:00
|
|
|
fwAppInstallTextView = (TextView) findViewById(R.id.debugTextView);
|
2015-04-06 20:58:35 +02:00
|
|
|
installButton = (Button) findViewById(R.id.installButton);
|
2015-08-14 23:37:47 +02:00
|
|
|
setInstallEnabled(false);
|
2015-04-20 20:49:14 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
|
|
|
filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
2015-04-06 20:58:35 +02:00
|
|
|
|
2015-04-20 20:49:14 +02:00
|
|
|
installButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2015-08-14 23:37:47 +02:00
|
|
|
setInstallEnabled(false);
|
2015-08-17 18:07:47 +02:00
|
|
|
installHandler.onStartInstall(device);
|
2015-08-04 01:01:14 +02:00
|
|
|
Intent startIntent = new Intent(FwAppInstallerActivity.this, DeviceCommunicationService.class);
|
|
|
|
startIntent.setAction(DeviceCommunicationService.ACTION_INSTALL);
|
2015-08-06 02:17:38 +02:00
|
|
|
startIntent.putExtra("uri", uri);
|
2015-04-20 20:49:14 +02:00
|
|
|
startService(startIntent);
|
|
|
|
}
|
|
|
|
});
|
2015-08-06 02:17:38 +02:00
|
|
|
|
|
|
|
uri = getIntent().getData();
|
|
|
|
installHandler = findInstallHandlerFor(uri);
|
|
|
|
if (installHandler == null) {
|
|
|
|
setInfoText(getString(R.string.installer_activity_unable_to_find_handler));
|
|
|
|
} else {
|
|
|
|
setInfoText(getString(R.string.installer_activity_wait_while_determining_status));
|
|
|
|
|
|
|
|
// needed to get the device
|
2015-08-06 21:35:00 +02:00
|
|
|
if (device == null || !device.isConnected()) {
|
|
|
|
connect();
|
2015-08-14 23:37:47 +02:00
|
|
|
} else {
|
|
|
|
Intent deviceInfoIntent = new Intent(this, DeviceCommunicationService.class);
|
|
|
|
deviceInfoIntent.setAction(DeviceCommunicationService.ACTION_REQUEST_DEVICEINFO);
|
|
|
|
startService(deviceInfoIntent);
|
2015-08-06 21:35:00 +02:00
|
|
|
}
|
2015-08-06 02:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private InstallHandler findInstallHandlerFor(Uri uri) {
|
|
|
|
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
|
|
|
|
InstallHandler handler = coordinator.findInstallHandler(uri, this);
|
|
|
|
if (handler != null) {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2015-04-20 20:49:14 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
2015-04-06 20:58:35 +02:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
2015-08-06 02:17:38 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInfoText(String text) {
|
|
|
|
fwAppInstallTextView.setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInstallEnabled(boolean enable) {
|
|
|
|
installButton.setEnabled(device != null && device.isConnected() && enable);
|
|
|
|
}
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|