2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2020-10-04 01:11:40 +02:00
|
|
|
Gobbetti, Lem Dulfo, Taavi Eomäe
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-07-28 17:30:20 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
2015-04-06 20:58:35 +02:00
|
|
|
|
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.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
|
|
|
|
2020-10-03 15:18:43 +02:00
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
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;
|
2018-08-06 23:13:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
2015-08-06 02:17:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-04-03 00:50:45 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
|
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
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public class FwAppInstallerActivity extends AbstractGBActivity 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);
|
2016-04-03 00:50:45 +02:00
|
|
|
private static final String ITEM_DETAILS = "details";
|
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
|
|
|
|
2020-10-03 15:18:43 +02:00
|
|
|
private ProgressBar progressBar;
|
2020-10-04 01:11:40 +02:00
|
|
|
private TextView progressText;
|
2016-04-03 00:50:45 +02:00
|
|
|
private ListView itemListView;
|
2020-10-03 15:18:43 +02:00
|
|
|
private final List<ItemWithDetails> items = new ArrayList<>();
|
|
|
|
private ItemWithDetailsAdapter itemAdapter;
|
2016-04-03 00:50:45 +02:00
|
|
|
|
|
|
|
private ListView detailsListView;
|
2020-10-03 15:18:43 +02:00
|
|
|
private ItemWithDetailsAdapter detailsAdapter;
|
|
|
|
private ArrayList<ItemWithDetails> details = new ArrayList<>();
|
2016-04-03 00:50:45 +02:00
|
|
|
|
2020-10-03 15:18:43 +02:00
|
|
|
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
2015-04-20 20:49:14 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2017-07-31 22:49:05 +02:00
|
|
|
if (GBDevice.ACTION_DEVICE_CHANGED.equals(action)) {
|
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
|
|
|
}
|
|
|
|
}
|
2020-10-04 01:11:40 +02:00
|
|
|
} else if (GB.ACTION_SET_PROGRESS_BAR.equals(action)) {
|
|
|
|
if (intent.hasExtra(GB.PROGRESS_BAR_INDETERMINATE)) {
|
|
|
|
setProgressIndeterminate(intent.getBooleanExtra(GB.PROGRESS_BAR_INDETERMINATE, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(GB.PROGRESS_BAR_PROGRESS)) {
|
|
|
|
setProgressIndeterminate(false);
|
|
|
|
setProgressBar(intent.getIntExtra(GB.PROGRESS_BAR_PROGRESS, 0));
|
|
|
|
}
|
|
|
|
} else if (GB.ACTION_SET_PROGRESS_TEXT.equals(action)) {
|
|
|
|
if (intent.hasExtra(GB.DISPLAY_MESSAGE_MESSAGE)) {
|
|
|
|
setProgressText(intent.getStringExtra(GB.DISPLAY_MESSAGE_MESSAGE));
|
|
|
|
}
|
|
|
|
} else if (GB.ACTION_SET_INFO_TEXT.equals(action)) {
|
|
|
|
if (intent.hasExtra(GB.DISPLAY_MESSAGE_MESSAGE)) {
|
|
|
|
setInfoText(intent.getStringExtra(GB.DISPLAY_MESSAGE_MESSAGE));
|
|
|
|
}
|
2016-04-03 00:50:45 +02:00
|
|
|
} else if (GB.ACTION_DISPLAY_MESSAGE.equals(action)) {
|
|
|
|
String message = intent.getStringExtra(GB.DISPLAY_MESSAGE_MESSAGE);
|
|
|
|
int severity = intent.getIntExtra(GB.DISPLAY_MESSAGE_SEVERITY, GB.INFO);
|
|
|
|
addMessage(message, severity);
|
2015-04-20 20:49:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-08-30 00:21:51 +02:00
|
|
|
|
|
|
|
private void refreshBusyState(GBDevice dev) {
|
|
|
|
if (dev.isConnecting() || dev.isBusy()) {
|
2020-10-03 15:18:43 +02:00
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
2015-08-30 00:21:51 +02:00
|
|
|
} else {
|
2020-10-03 15:18:43 +02:00
|
|
|
boolean wasBusy = progressBar.getVisibility() != View.GONE;
|
2015-08-30 00:21:51 +02:00
|
|
|
if (wasBusy) {
|
2020-10-03 15:18:43 +02:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2015-08-30 00:21:51 +02:00
|
|
|
// done!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 20:49:14 +02:00
|
|
|
|
2020-10-04 01:11:40 +02:00
|
|
|
public void setProgressIndeterminate(boolean indeterminate) {
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
progressBar.setIndeterminate(indeterminate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgressBar(int progress) {
|
|
|
|
progressBar.setProgress(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgressText(String text) {
|
|
|
|
progressText.setVisibility(View.VISIBLE);
|
|
|
|
progressText.setText(text);
|
|
|
|
}
|
|
|
|
|
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);
|
2016-04-10 00:13:25 +02:00
|
|
|
|
2015-08-06 21:35:00 +02:00
|
|
|
GBDevice dev = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
if (dev != null) {
|
|
|
|
device = dev;
|
|
|
|
}
|
2016-04-03 00:50:45 +02:00
|
|
|
if (savedInstanceState != null) {
|
2020-10-03 15:18:43 +02:00
|
|
|
details = savedInstanceState.getParcelableArrayList(ITEM_DETAILS);
|
|
|
|
if (details == null) {
|
|
|
|
details = new ArrayList<>();
|
2016-04-03 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
mayConnect = true;
|
2020-10-03 15:18:43 +02:00
|
|
|
itemListView = findViewById(R.id.itemListView);
|
|
|
|
itemAdapter = new ItemWithDetailsAdapter(this, items);
|
|
|
|
itemListView.setAdapter(itemAdapter);
|
|
|
|
fwAppInstallTextView = findViewById(R.id.infoTextView);
|
|
|
|
installButton = findViewById(R.id.installButton);
|
|
|
|
progressBar = findViewById(R.id.installProgressBar);
|
2020-10-04 01:11:40 +02:00
|
|
|
progressText = findViewById(R.id.installProgressText);
|
2020-10-03 15:18:43 +02:00
|
|
|
detailsListView = findViewById(R.id.detailsListView);
|
|
|
|
detailsAdapter = new ItemWithDetailsAdapter(this, details);
|
|
|
|
detailsAdapter.setSize(ItemWithDetailsAdapter.SIZE_SMALL);
|
|
|
|
detailsListView.setAdapter(detailsAdapter);
|
|
|
|
|
2015-08-14 23:37:47 +02:00
|
|
|
setInstallEnabled(false);
|
2015-04-20 20:49:14 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
2016-04-03 00:50:45 +02:00
|
|
|
filter.addAction(GB.ACTION_DISPLAY_MESSAGE);
|
2020-10-04 01:11:40 +02:00
|
|
|
filter.addAction(GB.ACTION_SET_PROGRESS_BAR);
|
|
|
|
filter.addAction(GB.ACTION_SET_PROGRESS_TEXT);
|
|
|
|
filter.addAction(GB.ACTION_SET_INFO_TEXT);
|
2020-10-03 15:18:43 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, 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();
|
2020-10-03 15:18:43 +02:00
|
|
|
if (uri == null) { // For "share" intent
|
2016-12-27 12:20:59 +01:00
|
|
|
uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
|
|
|
|
}
|
2015-08-06 02:17:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:50:45 +02:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
2020-10-03 15:18:43 +02:00
|
|
|
outState.putParcelableArrayList(ITEM_DETAILS, details);
|
2016-04-03 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
private InstallHandler findInstallHandlerFor(Uri uri) {
|
2018-08-06 23:13:25 +02:00
|
|
|
for (DeviceCoordinator coordinator : getAllCoordinatorsConnectedFirst()) {
|
2015-08-06 02:17:38 +02:00
|
|
|
InstallHandler handler = coordinator.findInstallHandler(uri, this);
|
|
|
|
if (handler != null) {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:13:25 +02:00
|
|
|
private List<DeviceCoordinator> getAllCoordinatorsConnectedFirst() {
|
|
|
|
DeviceManager deviceManager = ((GBApplication) getApplicationContext()).getDeviceManager();
|
|
|
|
List<DeviceCoordinator> connectedCoordinators = new ArrayList<>();
|
|
|
|
List<DeviceCoordinator> allCoordinators = DeviceHelper.getInstance().getAllCoordinators();
|
|
|
|
List<DeviceCoordinator> sortedCoordinators = new ArrayList<>(allCoordinators.size());
|
|
|
|
|
|
|
|
GBDevice connectedDevice = deviceManager.getSelectedDevice();
|
|
|
|
if (connectedDevice != null && connectedDevice.isConnected()) {
|
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(connectedDevice);
|
|
|
|
if (coordinator != null) {
|
|
|
|
connectedCoordinators.add(coordinator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedCoordinators.addAll(connectedCoordinators);
|
|
|
|
for (DeviceCoordinator coordinator : allCoordinators) {
|
|
|
|
if (!connectedCoordinators.contains(coordinator)) {
|
|
|
|
sortedCoordinators.add(coordinator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortedCoordinators;
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-03 15:18:43 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:45:30 +01:00
|
|
|
@Override
|
|
|
|
public CharSequence getInfoText() {
|
|
|
|
return fwAppInstallTextView.getText();
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
@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() {
|
2020-10-03 15:18:43 +02:00
|
|
|
items.clear();
|
|
|
|
itemAdapter.notifyDataSetChanged();
|
2015-08-30 00:21:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInstallItem(ItemWithDetails item) {
|
2020-10-03 15:18:43 +02:00
|
|
|
items.clear();
|
|
|
|
items.add(item);
|
|
|
|
itemAdapter.notifyDataSetChanged();
|
2015-08-30 00:21:51 +02:00
|
|
|
}
|
2016-04-03 00:50:45 +02:00
|
|
|
|
|
|
|
private void addMessage(String message, int severity) {
|
2020-10-03 15:18:43 +02:00
|
|
|
details.add(new GenericItem(message));
|
|
|
|
detailsAdapter.notifyDataSetChanged();
|
2016-04-03 00:50:45 +02:00
|
|
|
}
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|