Skip to content

Commit

Permalink
2.0.3 interim
Browse files Browse the repository at this point in the history
Added notification to Connector.java to fix forground service crashes.
  • Loading branch information
jroal committed Mar 31, 2019
1 parent 2b07c73 commit 6837794
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 38 deletions.
36 changes: 35 additions & 1 deletion app/app.iml
Expand Up @@ -17,7 +17,7 @@
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/build/generated/res/rs/debug;file://$MODULE_DIR$/build/generated/res/resValues/debug" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
</configuration>
</facet>
Expand Down Expand Up @@ -105,6 +105,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant_run_split_apk_resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifest-checker" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
Expand All @@ -121,11 +122,44 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/split-apk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/validate_signing_config" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: com.android.support:support-fragment:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:localbroadcastmanager:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:documentfile:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:animated-vector-drawable:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:slidingpanelayout:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:viewmodel:1.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:appcompat-v7:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:loader:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.core:runtime:1.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:livedata-core:1.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:collections:28.0.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:cursoradapter:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:runtime:1.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-compat:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-core-ui:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:asynclayoutinflater:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:print:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.core:common:1.1.1@jar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:versionedparcelable:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:viewpager:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-core-utils:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-vector-drawable:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-annotations:28.0.0@jar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:common:1.1.1@jar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:interpolator:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:livedata:1.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:drawerlayout:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:coordinatorlayout:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:customview:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:swiperefreshlayout:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-v4:28.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-media-compat:28.0.0@aar" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions app/build.gradle
Expand Up @@ -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'
}
}
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a2dp.connect2"
android:versionCode="8"
android:versionName="2.0.2">
android:versionCode="9"
android:versionName="2.0.3">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


<application
android:allowBackup="true"
android:icon="@drawable/icon"
Expand All @@ -23,6 +24,7 @@
<intent-filter>
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED" />
<!--<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />-->
</intent-filter>
</receiver>

Expand Down
28 changes: 12 additions & 16 deletions app/src/main/java/a2dp/connect2/BtReceiver.java
Expand Up @@ -6,29 +6,25 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import java.util.Objects;

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");
}
Expand Down
114 changes: 100 additions & 14 deletions app/src/main/java/a2dp/connect2/Connector.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -133,6 +145,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

sendIntent();
updatenot();
//connectBluetoothA2dp(bt_mac);

} else {
Expand All @@ -159,7 +172,6 @@ private static void sendIntent() {
application.sendBroadcast(intent);
}

;

private final BroadcastReceiver receiver = new BroadcastReceiver() {

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -281,12 +295,14 @@ private class ConnectBt extends AsyncTask<String, Void, Boolean> {
*/

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);
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);

}
}
}

0 comments on commit 6837794

Please sign in to comment.