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;
|
2015-08-30 00:21:51 +02:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.ProgressBar;
|
2015-04-06 20:58:35 +02:00
|
|
|
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-30 00:21:51 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-06 21:35:00 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-08-30 00:21:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.ItemWithDetailsAdapter;
|
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-30 00:21:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ItemWithDetails;
|
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-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
2015-04-20 20:49:14 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2015-12-07 23:33:32 +01:00
|
|
|
if (action.equals(GBApplication.ACTION_QUIT)) {
|
2015-04-20 20:49:14 +02:00
|
|
|
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) {
|
2015-08-30 00:21:51 +02:00
|
|
|
refreshBusyState(device);
|
2015-08-20 18:55:22 +02:00
|
|
|
if (!device.isInitialized()) {
|
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-30 00:21:51 +02:00
|
|
|
setInfoText(getString(R.string.fwappinstaller_connection_state, 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-30 00:21:51 +02:00
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
private ListView itemListView;
|
2015-11-23 23:04:46 +01:00
|
|
|
private final List<ItemWithDetails> mItems = new ArrayList<>();
|
2015-08-30 00:21:51 +02:00
|
|
|
private ItemWithDetailsAdapter mItemAdapter;
|
|
|
|
|
|
|
|
private void refreshBusyState(GBDevice dev) {
|
|
|
|
if (dev.isConnecting() || dev.isBusy()) {
|
|
|
|
mProgressBar.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
boolean wasBusy = mProgressBar.getVisibility() != View.GONE;
|
|
|
|
if (wasBusy) {
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
|
|
|
// done!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-12-13 00:43:07 +01:00
|
|
|
GBApplication.deviceService().connect(device);
|
2015-08-06 02:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-30 00:21:51 +02:00
|
|
|
itemListView = (ListView) findViewById(R.id.itemListView);
|
|
|
|
mItemAdapter = new ItemWithDetailsAdapter(this, mItems);
|
|
|
|
itemListView.setAdapter(mItemAdapter);
|
|
|
|
fwAppInstallTextView = (TextView) findViewById(R.id.infoTextView);
|
2015-04-06 20:58:35 +02:00
|
|
|
installButton = (Button) findViewById(R.id.installButton);
|
2015-08-30 00:21:51 +02:00
|
|
|
mProgressBar = (ProgressBar) findViewById(R.id.installProgressBar);
|
2015-08-14 23:37:47 +02:00
|
|
|
setInstallEnabled(false);
|
2015-04-20 20:49:14 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
2015-12-07 23:33:32 +01:00
|
|
|
filter.addAction(GBApplication.ACTION_QUIT);
|
2015-04-20 20:49:14 +02:00
|
|
|
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-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().onInstallApp(uri);
|
2015-04-20 20:49:14 +02:00
|
|
|
}
|
|
|
|
});
|
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 {
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().requestDeviceInfo();
|
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) {
|
2015-09-06 00:03:56 +02:00
|
|
|
boolean enabled = device != null && device.isConnected() && enable;
|
|
|
|
installButton.setEnabled(enabled);
|
|
|
|
installButton.setVisibility(enabled ? View.VISIBLE : View.GONE);
|
2015-08-06 02:17:38 +02:00
|
|
|
}
|
2015-08-30 00:21:51 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clearInstallItems() {
|
|
|
|
mItems.clear();
|
|
|
|
mItemAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInstallItem(ItemWithDetails item) {
|
|
|
|
mItems.clear();
|
|
|
|
mItems.add(item);
|
|
|
|
mItemAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|