2015-01-07 14:00:18 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
2015-03-07 15:32:34 +01:00
|
|
|
import android.app.Activity;
|
2015-03-21 18:18:07 +01:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-02-06 13:55:44 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.content.Intent;
|
2015-02-06 13:55:44 +01:00
|
|
|
import android.content.IntentFilter;
|
2015-01-18 22:44:38 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.os.Bundle;
|
2015-01-18 22:44:38 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-03-27 10:56:08 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2015-03-21 18:18:07 +01:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
2015-03-27 12:33:51 +01:00
|
|
|
import android.widget.TextView;
|
2015-03-21 18:18:07 +01:00
|
|
|
import android.widget.Toast;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2015-04-26 00:53:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2015-04-26 00:53:48 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
2015-05-05 00:48:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.discovery.DiscoveryActivity;
|
2015-04-26 00:53:48 +02:00
|
|
|
|
2015-03-07 15:32:34 +01:00
|
|
|
public class ControlCenter extends Activity {
|
2015-02-06 13:55:44 +01:00
|
|
|
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
public static final String ACTION_QUIT
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit";
|
2015-01-07 14:00:18 +01:00
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
public static final String ACTION_REFRESH_DEVICELIST
|
2015-03-22 00:34:54 +01:00
|
|
|
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.set_version";
|
|
|
|
|
2015-03-27 12:33:51 +01:00
|
|
|
TextView hintTextView;
|
2015-03-21 18:18:07 +01:00
|
|
|
ListView deviceListView;
|
|
|
|
GBDeviceAdapter mGBDeviceAdapter;
|
|
|
|
final List<GBDevice> deviceList = new ArrayList<>();
|
2015-01-07 14:00:18 +01:00
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-03-22 00:34:54 +01:00
|
|
|
String action = intent.getAction();
|
|
|
|
if (action.equals(ACTION_QUIT)) {
|
2015-02-06 13:55:44 +01:00
|
|
|
finish();
|
2015-04-20 11:58:59 +02:00
|
|
|
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
|
2015-04-19 12:35:23 +02:00
|
|
|
refreshPairedDevices();
|
2015-04-20 11:58:59 +02:00
|
|
|
} else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
|
|
|
GBDevice dev = intent.getParcelableExtra("device");
|
|
|
|
if (dev.getAddress() != null) {
|
2015-04-20 23:25:46 +02:00
|
|
|
int index = deviceList.indexOf(dev); // search by address
|
|
|
|
if (index >= 0) {
|
|
|
|
deviceList.set(index, dev);
|
2015-04-22 20:37:35 +02:00
|
|
|
} else {
|
|
|
|
deviceList.add(dev);
|
2015-03-22 00:34:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 11:58:59 +02:00
|
|
|
refreshPairedDevices();
|
2015-04-24 23:08:47 +02:00
|
|
|
|
|
|
|
if (dev.isConnected() && dev.getFirmwareVersion() == null) {
|
|
|
|
requestDeviceInfo();
|
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-07 14:00:18 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_controlcenter);
|
2015-03-28 23:23:10 +01:00
|
|
|
hintTextView = (TextView) findViewById(R.id.hintTextView);
|
2015-03-21 18:18:07 +01:00
|
|
|
deviceListView = (ListView) findViewById(R.id.deviceListView);
|
|
|
|
mGBDeviceAdapter = new GBDeviceAdapter(this, deviceList);
|
|
|
|
deviceListView.setAdapter(this.mGBDeviceAdapter);
|
|
|
|
deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
2015-01-12 00:35:15 +01:00
|
|
|
@Override
|
2015-03-21 18:18:07 +01:00
|
|
|
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
2015-04-19 15:53:43 +02:00
|
|
|
if (deviceList.get(position).isConnected()) {
|
2015-03-25 22:23:45 +01:00
|
|
|
Intent startIntent = new Intent(ControlCenter.this, AppManagerActivity.class);
|
|
|
|
startActivity(startIntent);
|
|
|
|
} else {
|
|
|
|
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT);
|
2015-05-05 00:48:02 +02:00
|
|
|
startIntent.putExtra(BluetoothCommunicationService.EXTRA_DEVICE_ADDRESS, deviceList.get(position).getAddress());
|
2015-03-25 22:23:45 +01:00
|
|
|
startService(startIntent);
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
|
|
|
});
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2015-03-22 00:34:54 +01:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ACTION_QUIT);
|
2015-03-22 12:46:28 +01:00
|
|
|
filter.addAction(ACTION_REFRESH_DEVICELIST);
|
2015-04-19 14:34:52 +02:00
|
|
|
filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
2015-03-27 10:56:08 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
refreshPairedDevices();
|
2015-01-18 22:44:38 +01:00
|
|
|
/*
|
|
|
|
* Ask for permission to intercept notifications on first run.
|
|
|
|
*/
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if (sharedPrefs.getBoolean("firstrun", true)) {
|
|
|
|
sharedPrefs.edit().putBoolean("firstrun", false).commit();
|
|
|
|
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
|
|
|
startActivity(enableIntent);
|
|
|
|
}
|
2015-03-22 00:34:54 +01:00
|
|
|
Intent startIntent = new Intent(this, BluetoothCommunicationService.class);
|
2015-02-06 13:55:44 +01:00
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_START);
|
|
|
|
startService(startIntent);
|
|
|
|
|
2015-04-24 23:08:47 +02:00
|
|
|
requestDeviceInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests information from the {@link BluetoothCommunicationService} about the connection state,
|
|
|
|
* firmware info, etc.
|
2015-04-26 00:53:48 +02:00
|
|
|
* <p/>
|
2015-04-24 23:08:47 +02:00
|
|
|
* Note that this method may cause an implicit device connection (for auto-connectable devices).
|
|
|
|
*/
|
|
|
|
private void requestDeviceInfo() {
|
|
|
|
Intent versionInfoIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
2015-03-22 00:34:54 +01:00
|
|
|
versionInfoIntent.setAction(BluetoothCommunicationService.ACTION_REQUEST_VERSIONINFO);
|
|
|
|
startService(versionInfoIntent);
|
2015-01-07 14:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2015-03-27 11:23:30 +01:00
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_settings:
|
|
|
|
Intent settingsIntent = new Intent(this, SettingsActivity.class);
|
|
|
|
startActivity(settingsIntent);
|
|
|
|
return true;
|
|
|
|
case R.id.action_debug:
|
|
|
|
Intent debugIntent = new Intent(this, DebugActivity.class);
|
|
|
|
startActivity(debugIntent);
|
|
|
|
return true;
|
|
|
|
case R.id.action_quit:
|
|
|
|
Intent stopIntent = new Intent(this, BluetoothCommunicationService.class);
|
|
|
|
stopService(stopIntent);
|
|
|
|
|
|
|
|
Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT);
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(quitIntent);
|
|
|
|
return true;
|
|
|
|
case R.id.action_refresh:
|
2015-04-19 12:35:23 +02:00
|
|
|
refreshPairedDevices();
|
2015-05-05 00:48:02 +02:00
|
|
|
return true;
|
|
|
|
case R.id.action_discover:
|
|
|
|
Intent discoverIntent = new Intent(this, DiscoveryActivity.class);
|
|
|
|
startActivity(discoverIntent);
|
|
|
|
return true;
|
2015-03-22 13:10:45 +01:00
|
|
|
}
|
2015-01-07 14:00:18 +01:00
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2015-03-27 10:56:08 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
2015-02-06 13:55:44 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
private void refreshPairedDevices() {
|
2015-04-20 23:25:46 +02:00
|
|
|
boolean connected = false;
|
|
|
|
List<GBDevice> availableDevices = new ArrayList<>();
|
2015-04-19 12:35:23 +02:00
|
|
|
for (GBDevice device : deviceList) {
|
2015-04-20 10:50:30 +02:00
|
|
|
if (device.isConnected() || device.isConnecting()) {
|
2015-04-20 23:25:46 +02:00
|
|
|
connected = true;
|
|
|
|
availableDevices.add(device);
|
2015-04-19 12:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
|
2015-04-19 12:35:23 +02:00
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
if (btAdapter == null) {
|
2015-05-01 01:26:12 +02:00
|
|
|
Toast.makeText(this, R.string.bluetooth_is_not_supported_, Toast.LENGTH_SHORT).show();
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (!btAdapter.isEnabled()) {
|
2015-05-01 01:26:12 +02:00
|
|
|
Toast.makeText(this, R.string.bluetooth_is_disabled_, Toast.LENGTH_SHORT).show();
|
2015-03-22 12:46:28 +01:00
|
|
|
} else {
|
|
|
|
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
2015-04-20 23:25:46 +02:00
|
|
|
for (BluetoothDevice pairedDevice : pairedDevices) {
|
2015-05-05 00:48:02 +02:00
|
|
|
DeviceType deviceDeviceType;
|
2015-04-20 23:25:46 +02:00
|
|
|
if (pairedDevice.getName().indexOf("Pebble") == 0) {
|
2015-05-05 00:48:02 +02:00
|
|
|
deviceDeviceType = DeviceType.PEBBLE;
|
2015-04-20 23:25:46 +02:00
|
|
|
} else if (pairedDevice.getName().equals("MI")) {
|
2015-05-05 00:48:02 +02:00
|
|
|
deviceDeviceType = DeviceType.MIBAND;
|
2015-04-01 18:34:52 +02:00
|
|
|
} else {
|
2015-03-31 23:34:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
GBDevice device = new GBDevice(pairedDevice.getAddress(), pairedDevice.getName(), deviceDeviceType);
|
2015-04-20 23:25:46 +02:00
|
|
|
if (!availableDevices.contains(device)) {
|
|
|
|
availableDevices.add(device);
|
2015-04-19 12:35:23 +02:00
|
|
|
}
|
2015-03-22 12:46:28 +01:00
|
|
|
}
|
2015-04-01 23:00:05 +02:00
|
|
|
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-05-05 00:48:02 +02:00
|
|
|
String miAddr = sharedPrefs.getString(GB.PREF_DEVELOPMENT_MIBAND_ADDRESS, null);
|
2015-04-20 23:25:46 +02:00
|
|
|
if (miAddr != null && miAddr.length() > 0) {
|
2015-05-05 00:48:02 +02:00
|
|
|
GBDevice miDevice = new GBDevice(miAddr, "MI", DeviceType.MIBAND);
|
2015-04-20 23:25:46 +02:00
|
|
|
if (!availableDevices.contains(miDevice)) {
|
|
|
|
availableDevices.add(miDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deviceList.retainAll(availableDevices);
|
|
|
|
for (GBDevice dev : availableDevices) {
|
|
|
|
if (!deviceList.contains(dev)) {
|
|
|
|
deviceList.add(dev);
|
|
|
|
}
|
2015-04-01 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 23:25:46 +02:00
|
|
|
if (connected) {
|
2015-05-01 01:26:12 +02:00
|
|
|
hintTextView.setText(R.string.tap_connected_device_for_app_mananger);
|
2015-04-19 12:35:23 +02:00
|
|
|
} else if (!deviceList.isEmpty()) {
|
2015-05-01 01:26:12 +02:00
|
|
|
hintTextView.setText(R.string.tap_a_device_to_connect);
|
2015-03-27 12:33:51 +01:00
|
|
|
}
|
2015-03-22 12:46:28 +01:00
|
|
|
}
|
2015-04-20 11:58:59 +02:00
|
|
|
mGBDeviceAdapter.notifyDataSetChanged();
|
2015-03-22 12:46:28 +01:00
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
}
|