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-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;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
2015-01-07 14:00:18 +01: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-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-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
|
2015-03-22 00:34:54 +01:00
|
|
|
String deviceAddress = intent.getStringExtra("device_address");
|
|
|
|
String firmwareVersion = intent.getStringExtra("firmware_version");
|
2015-03-22 12:46:28 +01:00
|
|
|
if (deviceList.isEmpty()) {
|
|
|
|
refreshPairedDevices();
|
|
|
|
mGBDeviceAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
if (deviceAddress != null) {
|
|
|
|
for (GBDevice device : deviceList) {
|
|
|
|
if (device.getAddress().equals(deviceAddress)) {
|
|
|
|
device.setFirmwareVersion(firmwareVersion);
|
|
|
|
mGBDeviceAdapter.notifyDataSetChanged();
|
|
|
|
break;
|
|
|
|
}
|
2015-03-22 00:34:54 +01:00
|
|
|
}
|
|
|
|
}
|
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-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-01-12 00:35:15 +01:00
|
|
|
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
2015-02-06 13:55:44 +01:00
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT);
|
2015-03-21 18:18:07 +01:00
|
|
|
startIntent.putExtra("device_address", deviceList.get(position).getAddress());
|
|
|
|
|
2015-01-12 00:35:15 +01:00
|
|
|
startService(startIntent);
|
|
|
|
}
|
|
|
|
});
|
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-03-22 00:34:54 +01:00
|
|
|
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-03-22 00:34:54 +01:00
|
|
|
Intent versionInfoIntent = new Intent(this, BluetoothCommunicationService.class);
|
|
|
|
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) {
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
|
|
if (id == R.id.action_settings) {
|
2015-03-06 14:00:56 +01:00
|
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
|
|
|
startActivity(intent);
|
2015-01-07 14:00:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-22 13:10:45 +01:00
|
|
|
else if (id == R.id.action_debug) {
|
2015-02-07 12:58:18 +01:00
|
|
|
Intent intent = new Intent(this, DebugActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-22 13:10:45 +01:00
|
|
|
else if (id == R.id.action_quit) {
|
|
|
|
Intent stopIntent = new Intent(this, BluetoothCommunicationService.class);
|
|
|
|
stopService(stopIntent);
|
|
|
|
|
|
|
|
Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT);
|
|
|
|
sendBroadcast(quitIntent);
|
|
|
|
}
|
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() {
|
|
|
|
super.onDestroy();
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
}
|
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
private void refreshPairedDevices() {
|
|
|
|
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
if (btAdapter == null) {
|
|
|
|
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
|
|
|
|
} else if (!btAdapter.isEnabled()) {
|
|
|
|
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
|
|
|
for (BluetoothDevice device : pairedDevices) {
|
|
|
|
if (device.getName().indexOf("Pebble") == 0) {
|
|
|
|
// Matching device found
|
|
|
|
deviceList.add(new GBDevice(device.getAddress(), device.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
}
|