2016-10-21 13:01:30 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.design.widget.FloatingActionButton;
|
|
|
|
import android.support.design.widget.NavigationView;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.support.v4.view.GravityCompat;
|
|
|
|
import android.support.v4.widget.DrawerLayout;
|
|
|
|
import android.support.v7.app.ActionBarDrawerToggle;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import de.cketti.library.changelog.ChangeLog;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapterv2;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
|
|
|
|
|
|
|
//TODO: extend GBActivity, but it requires actionbar that is not available
|
|
|
|
public class ControlCenterv2 extends AppCompatActivity
|
|
|
|
implements NavigationView.OnNavigationItemSelectedListener {
|
|
|
|
|
|
|
|
private DeviceManager deviceManager;
|
|
|
|
private ImageView background;
|
|
|
|
private TextView hintTextView;
|
|
|
|
|
|
|
|
private GBDeviceAdapterv2 mGBDeviceAdapter;
|
|
|
|
|
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
switch (action) {
|
|
|
|
case GBApplication.ACTION_QUIT:
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
case DeviceManager.ACTION_DEVICES_CHANGED:
|
|
|
|
refreshPairedDevices();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2016-10-21 23:01:10 +02:00
|
|
|
if (GBApplication.isDarkThemeEnabled()) {
|
|
|
|
setTheme(R.style.GadgetbridgeThemeDark_NoActionBar);
|
|
|
|
} else {
|
|
|
|
setTheme(R.style.GadgetbridgeTheme_NoActionBar);
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_controlcenterv2);
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
|
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
launchDiscoveryActivity();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
|
|
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
|
|
|
|
this, drawer, toolbar, R.string.controlcenter_navigation_drawer_open, R.string.controlcenter_navigation_drawer_close);
|
|
|
|
drawer.setDrawerListener(toggle);
|
|
|
|
toggle.syncState();
|
|
|
|
|
|
|
|
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
|
|
|
|
navigationView.setNavigationItemSelectedListener(this);
|
|
|
|
|
|
|
|
//end of material design boilerplate
|
|
|
|
deviceManager = GBApplication.getDeviceManager();
|
|
|
|
hintTextView = (TextView) findViewById(R.id.hintTextView);
|
|
|
|
ListView deviceListView = (ListView) findViewById(R.id.deviceListView);
|
|
|
|
background = (ImageView) findViewById(R.id.no_items_bg);
|
|
|
|
|
|
|
|
final List<GBDevice> deviceList = deviceManager.getDevices();
|
|
|
|
mGBDeviceAdapter = new GBDeviceAdapterv2(this, deviceList);
|
|
|
|
deviceListView.setAdapter(this.mGBDeviceAdapter);
|
|
|
|
deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
|
|
|
GBDevice gbDevice = mGBDeviceAdapter.getItem(position);
|
|
|
|
if (gbDevice.isInitialized()) {
|
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
|
|
|
Class<? extends Activity> primaryActivity = coordinator.getPrimaryActivity();
|
|
|
|
if (primaryActivity != null) {
|
|
|
|
Intent startIntent = new Intent(ControlCenterv2.this, primaryActivity);
|
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, gbDevice);
|
|
|
|
startActivity(startIntent);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GBApplication.deviceService().connect(gbDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registerForContextMenu(deviceListView);
|
|
|
|
|
|
|
|
IntentFilter filterLocal = new IntentFilter();
|
|
|
|
filterLocal.addAction(GBApplication.ACTION_QUIT);
|
|
|
|
filterLocal.addAction(DeviceManager.ACTION_DEVICES_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
|
|
|
|
|
|
|
|
refreshPairedDevices();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask for permission to intercept notifications on first run.
|
|
|
|
*/
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
if (prefs.getBoolean("firstrun", true)) {
|
|
|
|
prefs.getPreferences().edit().putBoolean("firstrun", false).apply();
|
|
|
|
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
|
|
|
startActivity(enableIntent);
|
|
|
|
}
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
checkAndRequestPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangeLog cl = new ChangeLog(this);
|
|
|
|
if (cl.isFirstRun()) {
|
|
|
|
cl.getLogDialog().show();
|
|
|
|
}
|
|
|
|
|
|
|
|
GBApplication.deviceService().start();
|
|
|
|
|
|
|
|
if (GB.isBluetoothEnabled() && deviceList.isEmpty() && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
|
startActivity(new Intent(this, DiscoveryActivity.class));
|
|
|
|
} else {
|
|
|
|
GBApplication.deviceService().requestDeviceInfo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
|
|
if (drawer.isDrawerOpen(GravityCompat.START)) {
|
|
|
|
drawer.closeDrawer(GravityCompat.START);
|
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onNavigationItemSelected(MenuItem item) {
|
|
|
|
|
|
|
|
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_db_management:
|
|
|
|
Intent dbIntent = new Intent(this, DbManagementActivity.class);
|
|
|
|
startActivity(dbIntent);
|
|
|
|
return true;
|
|
|
|
case R.id.action_quit:
|
|
|
|
GBApplication.quit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
|
|
drawer.closeDrawer(GravityCompat.START);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void launchDiscoveryActivity() {
|
|
|
|
startActivity(new Intent(this, DiscoveryActivity.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refreshPairedDevices() {
|
|
|
|
List<GBDevice> deviceList = deviceManager.getDevices();
|
|
|
|
GBDevice connectedDevice = null;
|
|
|
|
|
|
|
|
for (GBDevice device : deviceList) {
|
|
|
|
if (device.isConnected() || device.isConnecting()) {
|
|
|
|
connectedDevice = device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deviceList.isEmpty()) {
|
|
|
|
background.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
background.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectedDevice != null) {
|
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(connectedDevice);
|
|
|
|
hintTextView.setText(coordinator.getTapString());
|
|
|
|
} else if (!deviceList.isEmpty()) {
|
|
|
|
hintTextView.setText(R.string.tap_a_device_to_connect);
|
|
|
|
}
|
|
|
|
|
|
|
|
mGBDeviceAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
|
|
private void checkAndRequestPermissions() {
|
|
|
|
List<String> wantedPermissions = new ArrayList<>();
|
|
|
|
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.BLUETOOTH);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.BLUETOOTH_ADMIN);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.READ_CONTACTS);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.CALL_PHONE);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.READ_PHONE_STATE);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.PROCESS_OUTGOING_CALLS);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.READ_SMS);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.SEND_SMS);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add(Manifest.permission.READ_CALENDAR);
|
|
|
|
if (ContextCompat.checkSelfPermission(this, "com.fsck.k9.permission.READ_MESSAGES") == PackageManager.PERMISSION_DENIED)
|
|
|
|
wantedPermissions.add("com.fsck.k9.permission.READ_MESSAGES");
|
|
|
|
|
|
|
|
if (!wantedPermissions.isEmpty())
|
|
|
|
ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[wantedPermissions.size()]), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|