2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Lem Dulfo
|
|
|
|
|
|
|
|
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.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;
|
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
|
|
|
|
2016-04-14 15:21:25 +02:00
|
|
|
public class FwAppInstallerActivity extends GBActivity 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
|
|
|
|
2016-04-03 00:50:45 +02:00
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
private ListView itemListView;
|
|
|
|
private final List<ItemWithDetails> mItems = new ArrayList<>();
|
|
|
|
private ItemWithDetailsAdapter mItemAdapter;
|
|
|
|
|
|
|
|
private ListView detailsListView;
|
|
|
|
private ItemWithDetailsAdapter mDetailsItemAdapter;
|
|
|
|
private ArrayList<ItemWithDetails> mDetails = new ArrayList<>();
|
|
|
|
|
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();
|
2016-04-03 00:50:45 +02:00
|
|
|
if (GBApplication.ACTION_QUIT.equals(action)) {
|
2015-04-20 20:49:14 +02:00
|
|
|
finish();
|
2016-04-03 00:50:45 +02:00
|
|
|
} else 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
|
|
|
}
|
|
|
|
}
|
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()) {
|
|
|
|
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);
|
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) {
|
|
|
|
mDetails = savedInstanceState.getParcelableArrayList(ITEM_DETAILS);
|
|
|
|
if (mDetails == null) {
|
|
|
|
mDetails = new ArrayList<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2016-04-03 00:50:45 +02:00
|
|
|
detailsListView = (ListView) findViewById(R.id.detailsListView);
|
|
|
|
mDetailsItemAdapter = new ItemWithDetailsAdapter(this, mDetails);
|
|
|
|
mDetailsItemAdapter.setSize(ItemWithDetailsAdapter.SIZE_SMALL);
|
|
|
|
detailsListView.setAdapter(mDetailsItemAdapter);
|
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);
|
2016-04-03 00:50:45 +02:00
|
|
|
filter.addAction(GB.ACTION_DISPLAY_MESSAGE);
|
2015-04-20 20:49:14 +02:00
|
|
|
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();
|
2016-12-27 12:20:59 +01:00
|
|
|
if (uri == null) { //for "share" intent
|
|
|
|
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);
|
|
|
|
outState.putParcelableArrayList(ITEM_DETAILS, mDetails);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
mItems.clear();
|
|
|
|
mItemAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInstallItem(ItemWithDetails item) {
|
|
|
|
mItems.clear();
|
|
|
|
mItems.add(item);
|
|
|
|
mItemAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2016-04-03 00:50:45 +02:00
|
|
|
|
|
|
|
private void addMessage(String message, int severity) {
|
|
|
|
mDetails.add(new GenericItem(message));
|
|
|
|
mDetailsItemAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|