Skip to content
Permalink
Browse files

android >= 6.0 runtime permissions

  • Loading branch information...
AlexanderKub committed Apr 13, 2019
1 parent 820e1d5 commit ad7278356a0d08af35626b5ece6abac5b0821e6a
@@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
@@ -1,46 +1,53 @@
package com.alexanderkub.plugins.unitychirpio;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;

import io.chirp.connect.models.ChirpConnectState;

public final class Bridge {
private static ChirpPluginJavaMessageHandler javaMessageHandler;
private static Handler unityMainThreadHandler;
private ChirpConnector connector;

public void registerPluginHandlers(final Context ctx, ChirpPluginJavaMessageHandler handler) {
//region USED FROM UNITY METHODS
@SuppressWarnings("unused")
public void registerPluginHandlers(final Activity act,
ChirpPluginJavaMessageHandler handler) {
javaMessageHandler = handler;
if(unityMainThreadHandler == null) {
unityMainThreadHandler = new Handler();
}
this.connector = new ChirpConnector(ctx);
this.connector = new ChirpConnector(act.getBaseContext(), act);
}

@SuppressWarnings("unused")
public void ChirpInitSDK(String key, String secret, String config) {
this.connector.InitSDK(key, secret, config);
}

@SuppressWarnings("unused")
public int ChirpStartSDK() {
return this.connector.StartSDK();
}

@SuppressWarnings("unused")
public int ChirpStopSDK() {
return this.connector.StopSDK();
}

@SuppressWarnings("unused")
public int ChirpSendData(int length, String payload) {
return this.connector.sendPayload(length, payload);
return this.connector.sendPayload(payload);
}
//endregion

private static void runOnUnityThread(Runnable runnable) {
if(unityMainThreadHandler != null && runnable != null) {
unityMainThreadHandler.post(runnable);
}
}

protected static void SendReceiveEventToUnity(final String data) {
static void SendReceiveEventToUnity(final String data) {
runOnUnityThread(new Runnable() {
@Override
public void run() {
@@ -51,7 +58,7 @@ public void run() {
});
}

protected static void SendSentEventToUnity(final String data) {
static void SendSentEventToUnity(final String data) {
runOnUnityThread(new Runnable() {
@Override
public void run() {
@@ -62,7 +69,7 @@ public void run() {
});
}

protected static void SendChangeStateEventToUnity(int state) {
static void SendChangeStateEventToUnity(int state) {
final int tmpState = state;
runOnUnityThread(new Runnable() {
@Override
@@ -1,24 +1,29 @@
package com.alexanderkub.plugins.unitychirpio;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;
import android.support.annotation.NonNull;


import io.chirp.connect.ChirpConnect;
import io.chirp.connect.interfaces.ConnectEventListener;
import io.chirp.connect.interfaces.ConnectSetConfigListener;
import io.chirp.connect.models.ChirpError;
import io.chirp.connect.models.ChirpConnectState;

public class ChirpConnector {
class ChirpConnector {
private ChirpConnect chirpConnect;
private Context Ctx;
private Activity Act;

public ChirpConnector(final Context ctx) {
ChirpConnector(final Context ctx, final Activity act) {
this.Ctx = ctx;
this.Act = act;
}

protected void InitSDK(String key, String secret, String config) {
void InitSDK(String key, String secret, String config) {
if (this.chirpConnect != null) {
this.chirpConnect.close();
this.chirpConnect = null;
@@ -27,44 +32,34 @@ protected void InitSDK(String key, String secret, String config) {
final ConnectEventListener connectEventListener = new ConnectEventListener() {

@Override
public void onSending(byte[] payload, int channel) {
/**
* onSending is called when a send event begins.
* The data argument contains the payload being sent.
*/
String hexData = "null";
if (payload != null) {
hexData = new String(payload);
}
//Log.v("connectdemoapp", "ConnectCallback: onSending: " + hexData + " on channel: " + channel);
public void onSending(@NonNull byte[] payload, int channel) {
//String hexData = new String(payload);
//Log.v("connectDemoApp", "ConnectCallback: onSending: " + hexData + " on channel: " + channel);
}

@Override
public void onSent(byte[] data, int channel) {
/**
public void onSent(@NonNull byte[] data, int channel) {
/*
* onSent is called when a send event has completed.
* The data argument contains the payload that was sent.
*/
String hexData = "null";
if (data != null) {
hexData = new String(data);
}
String hexData = new String(data);
Bridge.SendSentEventToUnity(hexData);
//Log.v("connectdemoapp", "ConnectCallback: onSent: " + hexData + " on channel: " + channel);
//Log.v("connectDemoApp", "ConnectCallback: onSent: " + hexData + " on channel: " + channel);
}

@Override
public void onReceiving(int channel) {
/**
/*
* onReceiving is called when a receive event begins.
* No data has yet been received.
*/
//Log.v("connectdemoapp", "ConnectCallback: onReceiving on channel: " + channel);
//Log.v("connectDemoApp", "ConnectCallback: onReceiving on channel: " + channel);
}

@Override
public void onReceived(byte[] data, int channel) {
/**
/*
* onReceived is called when a receive event has completed.
* If the payload was decoded successfully, it is passed in data.
* Otherwise, data is null.
@@ -74,20 +69,20 @@ public void onReceived(byte[] data, int channel) {
hexData = new String(data);
}
Bridge.SendReceiveEventToUnity(hexData);
//Log.v("connectdemoapp", "ConnectCallback: onReceived: " + hexData + " on channel: " + channel);
//Log.v("connectDemoApp", "ConnectCallback: onReceived: " + hexData + " on channel: " + channel);
}

@Override
public void onStateChanged(int oldState, int newState) {
/**
/*
* onStateChanged is called when the SDK changes state.
*/
Bridge.SendChangeStateEventToUnity(newState);
}

@Override
public void onSystemVolumeChanged(int oldVolume, int newVolume) {
/**
/*
* onSystemVolumeChanged is called when the system volume is changed.
*/
}
@@ -101,9 +96,20 @@ public void onSystemVolumeChanged(int oldVolume, int newVolume) {
} else {
Log.e("ChirpError: ", error.getMessage());
}
GrantPermissions();
}

@TargetApi(23)
private static final int RESULT_REQUEST_RECORD_AUDIO = 1095;

@TargetApi(23)
private void GrantPermissions() {
if (Ctx.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
Act.requestPermissions(new String[] {Manifest.permission.RECORD_AUDIO}, RESULT_REQUEST_RECORD_AUDIO);
}
}

public int StartSDK() {
int StartSDK() {
if (this.chirpConnect == null) {
return 404;
}
@@ -115,7 +121,7 @@ public int StartSDK() {
return 0;
}

public int StopSDK() {
int StopSDK() {
if (this.chirpConnect == null) {
return 404;
}
@@ -128,16 +134,16 @@ public int StopSDK() {
}


protected int sendPayload(int length, String message) {
/**
int sendPayload(String message) {
/*
* A payload is a byte array dynamic size with a maximum size defined by the config string.
*
*/
byte[] payload = message.getBytes();

long maxSize = chirpConnect.maxPayloadLength();
if (maxSize < payload.length) {
return 83;
return 84;
}
ChirpError error = chirpConnect.send(payload);
int errorCode = error.getCode();
BIN +311 Bytes (100%) UnityChirpIOPlugin.unitypackage
Binary file not shown.

0 comments on commit ad72783

Please sign in to comment.
You can’t perform that action at this time.