diff --git a/app/app.iml b/app/app.iml
index d65caba..45420ab 100644
--- a/app/app.iml
+++ b/app/app.iml
@@ -17,7 +17,7 @@
-
+
@@ -105,6 +105,7 @@
+
@@ -121,11 +122,44 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/build.gradle b/app/build.gradle
index 3c3a6b9..39d0b1e 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -34,4 +34,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
+
+ dependencies {
+ implementation 'com.android.support:support-v4:28.0.0'
+ implementation 'com.android.support:appcompat-v7:28.0.0'
+ }
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9884dac..da27c05 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,12 +1,13 @@
+ android:versionCode="9"
+ android:versionName="2.0.3">
+
+
diff --git a/app/src/main/java/a2dp/connect2/BtReceiver.java b/app/src/main/java/a2dp/connect2/BtReceiver.java
index 53f2493..1951612 100644
--- a/app/src/main/java/a2dp/connect2/BtReceiver.java
+++ b/app/src/main/java/a2dp/connect2/BtReceiver.java
@@ -6,6 +6,7 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import android.os.Build;
import java.util.Objects;
@@ -13,22 +14,17 @@ public class BtReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
- Context application = Connector.application;
- Intent intent2 = new Intent(application, WidgetProvider.class);
- intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
-
- int[] ids = AppWidgetManager.getInstance(application).getAppWidgetIds(new ComponentName(application, WidgetProvider.class));
- BluetoothDevice bt;
- try {
- bt = (BluetoothDevice) Objects.requireNonNull(intent.getExtras()).get(
- BluetoothDevice.EXTRA_DEVICE);
- } catch (Exception e1) {
- bt = null;
- e1.printStackTrace();
- }
- intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
- intent2.putExtra("BT", bt.getAddress());
- application.sendBroadcast(intent2);
+
+ Intent intent2 = new Intent(context, RunUpdate.class);
+
+ context.startService(intent2);
+
+/* if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
+ context.startService(intent2);
+ } else {
+ context.startForegroundService(intent2);
+ }*/
+
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
diff --git a/app/src/main/java/a2dp/connect2/Connector.java b/app/src/main/java/a2dp/connect2/Connector.java
index ed9c1b8..a5577cf 100644
--- a/app/src/main/java/a2dp/connect2/Connector.java
+++ b/app/src/main/java/a2dp/connect2/Connector.java
@@ -4,6 +4,10 @@
import java.lang.reflect.Method;
import java.util.Set;
+import android.annotation.TargetApi;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothA2dp;
@@ -21,10 +25,14 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
+import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.RemoteException;
+import android.support.annotation.RequiresApi;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
@@ -40,10 +48,25 @@ public class Connector extends Service {
public static Context application;
private static String DeviceToConnect;
+ private static final String A2DP_FOREGROUND = "a2dp_foreground";
+ NotificationChannel channel_f;
+ static final int ENABLE_BLUETOOTH = 1;
+ private String PREFS = "bluetoothlauncher";
+ private static String LOG_TAG = "A2DP_Connect";
+ private BluetoothDevice device = null;
+ private String dname;
+ private String bt_mac;
+ boolean serviceRegistered = false;
+ boolean receiverRegistered = false;
+ private NotificationManager mNotificationManager = null;
+ private NotificationManagerCompat notificationManagerCompat = null;
+
+ int w_id;
@Override
public void onDestroy() {
//this.unregisterReceiver(receiver);
+
Log.i(LOG_TAG, "OnDestroy called");
done();
super.onDestroy();
@@ -52,21 +75,10 @@ public void onDestroy() {
@Override
protected void finalize() throws Throwable {
-
+ done();
super.finalize();
}
- static final int ENABLE_BLUETOOTH = 1;
- private String PREFS = "bluetoothlauncher";
- private static String LOG_TAG = "A2DP_Connect";
- private BluetoothDevice device = null;
- private String dname;
- private String bt_mac;
- boolean serviceRegistered = false;
- boolean receiverRegistered = false;
-
- int w_id;
-
@Override
public IBinder onBind(Intent intent) {
return null;
@@ -133,6 +145,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
sendIntent();
+ updatenot();
//connectBluetoothA2dp(bt_mac);
} else {
@@ -159,7 +172,6 @@ private static void sendIntent() {
application.sendBroadcast(intent);
}
- ;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@@ -195,6 +207,8 @@ public void onReceive(Context arg0, Intent arg1) {
public void onCreate() {
// super.onCreate();
application = getApplication();
+ //mNotificationManager = getSystemService(NotificationManager.class);
+ notificationManagerCompat = NotificationManagerCompat.from(application);
if (!receiverRegistered) {
String filter_1_string = "a2dp.connect2.Connector.INTERFACE";
@@ -281,12 +295,14 @@ private class ConnectBt extends AsyncTask {
*/
String btd;
+
@Override
protected void onPostExecute(Boolean result) {
Intent intent = new Intent(application, RunUpdate.class);
- intent.putExtra("BT", btd );
+ intent.putExtra("BT", btd);
application.startService(intent);
+ done();
super.onPostExecute(result);
}
@@ -339,11 +355,13 @@ protected Boolean doInBackground(String... arg0) {
}
+
private void done() {
Log.i(LOG_TAG, "Service stopping");
if (receiverRegistered) {
try {
application.unregisterReceiver(receiver);
+ receiverRegistered = false;
} catch (Exception e) {
e.printStackTrace();
}
@@ -355,9 +373,77 @@ private void done() {
e.printStackTrace();
}
}
+
+ mNotificationManager.cancel(1);
+ mNotificationManager.cancelAll();
+ notificationManagerCompat.cancelAll();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ this.stopForeground(Service.STOP_FOREGROUND_REMOVE);
+ }
+ this.stopForeground(true);
this.stopSelf();
}
+ private void updatenot() {
+ if (channel_f == null) createNotificationChannel();
+ if (mNotificationManager != null) {
+ mNotificationManager.cancelAll();
+ } else {
+ createNotificationChannel();
+ }
+ if (notificationManagerCompat != null) {
+ notificationManagerCompat.cancelAll();
+ } else {
+ createNotificationChannel();
+ }
+
+ String temp = "";
+ Notification not = null;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ not = new NotificationCompat.Builder(application, A2DP_FOREGROUND)
+ .setContentTitle(
+ getResources().getString(R.string.app_name))
+
+ .setSmallIcon(R.drawable.icon)
+ .setCategory(Notification.CATEGORY_SERVICE)
+ .setContentText(temp)
+ .setChannelId(A2DP_FOREGROUND).build();
+ notificationManagerCompat.notify(1, not);
+ //Toast.makeText(application, "Test on " + car + " " +not.getChannelId(), Toast.LENGTH_LONG).show();
+ } else {
+ not = new NotificationCompat.Builder(application, LAUNCHER_APPS_SERVICE)
+ .setContentTitle(
+ getResources().getString(R.string.app_name))
+ //.setContentIntent(contentIntent)
+ .setSmallIcon(R.drawable.icon)
+ .setContentText(temp)
+ .setPriority(Notification.PRIORITY_LOW)
+ .build();
+ notificationManagerCompat.notify(1, not);
+ }
+ this.startForeground(1, not);
+ }
+
+ @TargetApi(Build.VERSION_CODES.M)
+ private void createNotificationChannel() {
+ mNotificationManager = getSystemService(NotificationManager.class);
+ //notificationManagerCompat = NotificationManagerCompat.from(application);
+
+ // Create the NotificationChannel, but only on API 26+ because
+ // the NotificationChannel class is new and not in the support library
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+
+ // set up foreground notification channel
+ CharSequence name2 = getString(R.string.foreground_channel_name);
+ String description2 = getString(R.string.foreground_channel_description);
+ int importance2 = NotificationManager.IMPORTANCE_LOW;
+ channel_f = new NotificationChannel(A2DP_FOREGROUND, name2, importance2);
+ channel_f.setDescription(description2);
+ // Register the channel with the system; you can't change the importance
+ // or other notification behaviors after this
+ mNotificationManager.createNotificationChannel(channel_f);
+ }
+ }
}
diff --git a/app/src/main/java/a2dp/connect2/RunUpdate.java b/app/src/main/java/a2dp/connect2/RunUpdate.java
index 0f713a5..e6631c1 100644
--- a/app/src/main/java/a2dp/connect2/RunUpdate.java
+++ b/app/src/main/java/a2dp/connect2/RunUpdate.java
@@ -1,5 +1,9 @@
package a2dp.connect2;
+import android.annotation.TargetApi;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothA2dp;
@@ -12,12 +16,16 @@
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
+import android.os.Build;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.RemoteException;
+import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.NotificationManagerCompat;
public class RunUpdate extends Service {
@@ -34,6 +42,10 @@ public void onDestroy() {
static SharedPreferences preferences;
static Context application;
static RemoteViews views;
+ private NotificationManager mNotificationManager = null;
+ private NotificationManagerCompat notificationManagerCompat = null;
+ private static final String A2DP_FOREGROUND = "a2dp_foreground";
+ NotificationChannel channel_f;
public RunUpdate() {
@@ -92,6 +104,8 @@ public void onCreate() {
preferences = getApplicationContext().getSharedPreferences(PREFS, 0);
views = new RemoteViews(this.getPackageName(),
R.layout.widget_initial_layout);
+ if(!mIsBound) getIBluetoothA2dp(this);
+ notificationManagerCompat = NotificationManagerCompat.from(application);
super.onCreate();
}
@@ -144,10 +158,12 @@ public static boolean isDeviceConnected(String btd) {
int sinkState = 0;
- try {
- sinkState = ibta2.getConnectionState(device);
- } catch (RemoteException e) {
- e.printStackTrace();
+ if (device != null) {
+ try {
+ sinkState = ibta2.getConnectionState(device);
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ }
}
Boolean connected = sinkState == BluetoothA2dp.STATE_CONNECTED || sinkState == BluetoothA2dp.STATE_CONNECTING;
@@ -171,4 +187,68 @@ public static void doUnbindService(Context context) {
}
+
+// This will only be needed if I ever start this as a foreground service.
+
+ private void updatenot() {
+ if (channel_f == null) createNotificationChannel();
+ if (mNotificationManager != null) {
+ mNotificationManager.cancelAll();
+ } else {
+ createNotificationChannel();
+ }
+ if (notificationManagerCompat != null) {
+ notificationManagerCompat.cancelAll();
+ } else {
+ createNotificationChannel();
+ }
+
+ String temp = "";
+ Notification not = null;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ not = new NotificationCompat.Builder(application, A2DP_FOREGROUND)
+ .setContentTitle(
+ getResources().getString(R.string.app_name))
+
+ .setSmallIcon(R.drawable.icon)
+ .setCategory(Notification.CATEGORY_SERVICE)
+ .setContentText(temp)
+ .setChannelId(A2DP_FOREGROUND).build();
+ notificationManagerCompat.notify(1, not);
+ //Toast.makeText(application, "Test on " + car + " " +not.getChannelId(), Toast.LENGTH_LONG).show();
+ } else {
+ not = new NotificationCompat.Builder(application, LAUNCHER_APPS_SERVICE)
+ .setContentTitle(
+ getResources().getString(R.string.app_name))
+ //.setContentIntent(contentIntent)
+ .setSmallIcon(R.drawable.icon)
+ .setContentText(temp)
+ .setPriority(Notification.PRIORITY_LOW)
+ .build();
+ notificationManagerCompat.notify(1, not);
+ }
+ this.startForeground(1, not);
+ }
+
+ @TargetApi(Build.VERSION_CODES.M)
+ private void createNotificationChannel() {
+ mNotificationManager = getSystemService(NotificationManager.class);
+ //notificationManagerCompat = NotificationManagerCompat.from(application);
+
+ // Create the NotificationChannel, but only on API 26+ because
+ // the NotificationChannel class is new and not in the support library
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+
+ // set up foreground notification channel
+ CharSequence name2 = getString(R.string.foreground_channel_name);
+ String description2 = getString(R.string.foreground_channel_description);
+ int importance2 = NotificationManager.IMPORTANCE_LOW;
+ channel_f = new NotificationChannel(A2DP_FOREGROUND, name2, importance2);
+ channel_f.setDescription(description2);
+ // Register the channel with the system; you can't change the importance
+ // or other notification behaviors after this
+ mNotificationManager.createNotificationChannel(channel_f);
+
+ }
+ }
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 217fb8f..ac03253 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -11,4 +11,6 @@
Bluetooth must be enabled before you can place a widget
Bluetooth not enabled
Interface error
+ Foreground Channel
+ Foreground notification channel
diff --git a/build.gradle b/build.gradle
index c1949b9..9d4ac1e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ buildscript {
google()
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.3.1'
+ classpath 'com.android.tools.build:gradle:3.3.2'
}
}