package nodomain.freeyourgadget.gadgetbridge; import android.app.Activity; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class ControlCenter extends Activity { public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit"; Button startServiceButton; private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_QUIT)) { finish(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_controlcenter); registerReceiver(mReceiver, new IntentFilter(ACTION_QUIT)); startServiceButton = (Button) findViewById(R.id.startServiceButton); startServiceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class); startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); startService(startIntent); } }); /* * Ask for permission to intercept notifications on first run. * TODO: allow re-request in preferences */ 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); } Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class); startIntent.setAction(BluetoothCommunicationService.ACTION_START); startService(startIntent); } private void testNotification() { NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this); ncomp.setContentTitle("Test Notification"); ncomp.setContentText("This is a Test Notification from Gadgetbridge"); ncomp.setTicker("This is a Test Notification from Gadgetbridge"); ncomp.setSmallIcon(R.drawable.ic_launcher); ncomp.setAutoCancel(true); nManager.notify((int) System.currentTimeMillis(), ncomp.build()); } @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) { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); return true; } if (id == R.id.action_debug) { Intent intent = new Intent(this, DebugActivity.class); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); } }