diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index e287fe107..8aeb78c2a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -1,11 +1,16 @@ package nodomain.freeyourgadget.gadgetbridge; import android.app.Application; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Build; import android.os.Build.VERSION; import android.preference.PreferenceManager; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import org.slf4j.Logger; @@ -20,6 +25,7 @@ import java.util.concurrent.locks.ReentrantLock; import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; @@ -38,6 +44,24 @@ public class GBApplication extends Application { private static DeviceService deviceService; private static SharedPreferences sharedPrefs; + public static final String ACTION_QUIT + = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + switch (action) { + case ACTION_QUIT: + quit(); + break; + } + } + }; + + private void quit() { + GB.removeAllNotifications(this); + } + public GBApplication() { context = this; // don't do anything here, add it to onCreate instead @@ -68,6 +92,11 @@ public class GBApplication extends Application { GB.environment = GBEnvironment.createDeviceEnvironment(); mActivityDatabaseHandler = new ActivityDatabaseHandler(context); loadBlackList(); + + IntentFilter filterLocal = new IntentFilter(); + filterLocal.addAction(ACTION_QUIT); + LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); + // for testing DB stuff // SQLiteDatabase db = mActivityDatabaseHandler.getWritableDatabase(); // db.close(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java index b3ead6b14..3056de51b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java @@ -39,7 +39,7 @@ public class AppBlacklistActivity extends Activity { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(ControlCenter.ACTION_QUIT)) { + if (action.equals(GBApplication.ACTION_QUIT)) { finish(); } } @@ -99,7 +99,7 @@ public class AppBlacklistActivity extends Activity { }); IntentFilter filter = new IntentFilter(); - filter.addAction(ControlCenter.ACTION_QUIT); + filter.addAction(GBApplication.ACTION_QUIT); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java index 69bf7c3ac..45996f92a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java @@ -42,7 +42,7 @@ public class AppManagerActivity extends Activity { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(ControlCenter.ACTION_QUIT)) { + if (action.equals(GBApplication.ACTION_QUIT)) { finish(); } else if (action.equals(ACTION_REFRESH_APPLIST)) { appList.clear(); @@ -139,7 +139,7 @@ public class AppManagerActivity extends Activity { } IntentFilter filter = new IntentFilter(); - filter.addAction(ControlCenter.ACTION_QUIT); + filter.addAction(GBApplication.ACTION_QUIT); filter.addAction(ACTION_REFRESH_APPLIST); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java index de775bae0..8bd609dc0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java @@ -45,9 +45,6 @@ public class ControlCenter extends Activity { private static final Logger LOG = LoggerFactory.getLogger(ControlCenter.class); - public static final String ACTION_QUIT - = "nodomain.freeyourgadget.gadgetbridge.controlcenter.action.quit"; - public static final String ACTION_REFRESH_DEVICELIST = "nodomain.freeyourgadget.gadgetbridge.controlcenter.action.set_version"; @@ -63,7 +60,7 @@ public class ControlCenter extends Activity { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { - case ACTION_QUIT: + case GBApplication.ACTION_QUIT: finish(); break; case ACTION_REFRESH_DEVICELIST: @@ -155,7 +152,7 @@ public class ControlCenter extends Activity { registerForContextMenu(deviceListView); IntentFilter filterLocal = new IntentFilter(); - filterLocal.addAction(ACTION_QUIT); + filterLocal.addAction(GBApplication.ACTION_QUIT); filterLocal.addAction(ACTION_REFRESH_DEVICELIST); filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED); filterLocal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); @@ -316,7 +313,7 @@ public class ControlCenter extends Activity { case R.id.action_quit: GBApplication.deviceService().quit(); - Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT); + Intent quitIntent = new Intent(GBApplication.ACTION_QUIT); LocalBroadcastManager.getInstance(this).sendBroadcast(quitIntent); return true; case R.id.action_discover: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index 8c30bd969..e5a90d0cf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -53,7 +53,7 @@ public class DebugActivity extends Activity { private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(ControlCenter.ACTION_QUIT)) { + if (intent.getAction().equals(GBApplication.ACTION_QUIT)) { finish(); } } @@ -65,7 +65,7 @@ public class DebugActivity extends Activity { setContentView(R.layout.activity_debug); getActionBar().setDisplayHomeAsUpEnabled(true); - registerReceiver(mReceiver, new IntentFilter(ControlCenter.ACTION_QUIT)); + registerReceiver(mReceiver, new IntentFilter(GBApplication.ACTION_QUIT)); editContent = (EditText) findViewById(R.id.editContent); sendSMSButton = (Button) findViewById(R.id.sendSMSButton); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java index 19a457862..e3baad523 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java @@ -49,7 +49,7 @@ public class FwAppInstallerActivity extends Activity implements InstallActivity @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(ControlCenter.ACTION_QUIT)) { + if (action.equals(GBApplication.ACTION_QUIT)) { finish(); } else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) { device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); @@ -116,7 +116,7 @@ public class FwAppInstallerActivity extends Activity implements InstallActivity mProgressBar = (ProgressBar) findViewById(R.id.installProgressBar); setInstallEnabled(false); IntentFilter filter = new IntentFilter(); - filter.addAction(ControlCenter.ACTION_QUIT); + filter.addAction(GBApplication.ACTION_QUIT); filter.addAction(GBDevice.ACTION_DEVICE_CHANGED); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java index c3c755026..f09fca794 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java @@ -32,7 +32,6 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdapter; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragmentActivity; -import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; @@ -85,7 +84,7 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { - case ControlCenter.ACTION_QUIT: + case GBApplication.ACTION_QUIT: finish(); break; case GBDevice.ACTION_DEVICE_CHANGED: @@ -119,7 +118,7 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts initDates(); IntentFilter filterLocal = new IntentFilter(); - filterLocal.addAction(ControlCenter.ACTION_QUIT); + filterLocal.addAction(GBApplication.ACTION_QUIT); filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java index 22d06fd4f..74a3d2dcf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java @@ -31,7 +31,7 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver { } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { GBApplication.deviceService().quit(); - Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT); + Intent quitIntent = new Intent(GBApplication.ACTION_QUIT); LocalBroadcastManager.getInstance(context).sendBroadcast(quitIntent); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index 9ffa22f1c..7571badc7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -266,6 +266,12 @@ public class GB { return nb.build(); } + public static void removeAllNotifications(Context context) { + removeNotification(NOTIFICATION_ID_TRANSFER, context); + removeNotification(NOTIFICATION_ID_INSTALL, context); + removeNotification(NOTIFICATION_ID_LOW_BATTERY, context); + } + public static void updateTransferNotification(String text, boolean ongoing, int percentage, Context context) { if (percentage == 100) { removeNotification(NOTIFICATION_ID_TRANSFER, context);