2019-10-18 00:02:53 +02:00
|
|
|
/* Copyright (C) 2018-2019 Daniele Gobbetti, maxirnilian
|
|
|
|
|
|
|
|
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/>. */
|
2019-10-23 22:07:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.lenovo;
|
2019-10-18 00:02:53 +02:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2019-12-13 22:45:38 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2019-10-18 00:02:53 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
2019-10-23 22:07:38 +02:00
|
|
|
public class LenovoWatchPairingActivity extends AbstractGBActivity {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(LenovoWatchPairingActivity.class);
|
2019-10-18 00:02:53 +02:00
|
|
|
|
|
|
|
private static final String STATE_DEVICE_CANDIDATE = "stateDeviceCandidate";
|
|
|
|
|
|
|
|
private TextView message;
|
|
|
|
private GBDeviceCandidate deviceCandidate;
|
|
|
|
|
|
|
|
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
|
|
|
|
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
LOG.debug("pairing activity: device changed: " + device);
|
|
|
|
if (deviceCandidate.getMacAddress().equals(device.getAddress())) {
|
|
|
|
if (device.isInitialized()) {
|
|
|
|
pairingFinished();
|
|
|
|
} else if (device.isConnecting() || device.isInitializing()) {
|
|
|
|
LOG.info("still connecting/initializing device...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_watch9_pairing);
|
|
|
|
|
|
|
|
message = findViewById(R.id.watch9_pair_message);
|
|
|
|
Intent intent = getIntent();
|
|
|
|
deviceCandidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
|
|
|
|
if (deviceCandidate == null && savedInstanceState != null) {
|
|
|
|
deviceCandidate = savedInstanceState.getParcelable(STATE_DEVICE_CANDIDATE);
|
|
|
|
}
|
|
|
|
if (deviceCandidate == null) {
|
|
|
|
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
|
|
|
|
startActivity(new Intent(this, DiscoveryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startPairing();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-13 22:45:38 +01:00
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
2019-10-18 00:02:53 +02:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putParcelable(STATE_DEVICE_CANDIDATE, deviceCandidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
deviceCandidate = savedInstanceState.getParcelable(STATE_DEVICE_CANDIDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startPairing() {
|
|
|
|
message.setText(getString(R.string.pairing, deviceCandidate));
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
|
|
|
|
|
|
|
|
GBApplication.deviceService().disconnect();
|
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
|
|
|
|
if (device != null) {
|
|
|
|
GBApplication.deviceService().connect(device, true);
|
|
|
|
} else {
|
|
|
|
GB.toast(this, "Unable to connect, can't recognize the device type: " + deviceCandidate, Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void pairingFinished() {
|
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
|
|
|
|
|
|
|
Intent intent = new Intent(this, ControlCenterv2.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|