Skip to content

Commit

Permalink
Merge pull request #377 from ytai/AndroidStudioJellyfish
Browse files Browse the repository at this point in the history
Android Studio Jellyfish
  • Loading branch information
hannesa2 authored May 10, 2024
2 parents a5f9107 + 2fdab3d commit 6e177d1
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 58 deletions.
12 changes: 6 additions & 6 deletions IOIOLibAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ dependencies {
api 'androidx.appcompat:appcompat:1.6.1'
}

project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
publishing {
publications {
release(MavenPublication) {
afterEvaluate {
from components.findByName('release')
}
}
}
}
}
10 changes: 5 additions & 5 deletions IOIOLibAndroidAccessory/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ dependencies {
api project(":IOIOLibAndroid")
}

project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
publishing {
publications {
release(MavenPublication) {
afterEvaluate {
from components.findByName('release')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.Log;

Expand Down Expand Up @@ -154,7 +155,7 @@ public Object getExtra() {
return null;
}

private synchronized void waitForConnect(Connection connection) throws ConnectionLostException {
private synchronized void waitForConnect() throws ConnectionLostException {
// In order to simplify the connection process in face of many different sequences of events
// that might occur, we collapsed the entire sequence into one non-blocking method,
// tryOpen(), which tries the entire process from the beginning, undoes everything if
Expand Down Expand Up @@ -196,8 +197,15 @@ private boolean tryOpen() {
if (!usbManager_.hasPermission(accessory)) {
if (pendingIntent_ == null) {
Log.v(TAG, "Requesting permission.");
pendingIntent_ = PendingIntent.getBroadcast(activity_, 0, new Intent(
ACTION_USB_PERMISSION), 0);

int pendingIntentFlags;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
} else {
pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
}

pendingIntent_ = PendingIntent.getBroadcast(activity_, 0, new Intent(ACTION_USB_PERMISSION), pendingIntentFlags);
usbManager_.requestPermission(accessory, pendingIntent_);
}
return false;
Expand Down Expand Up @@ -233,7 +241,7 @@ private boolean tryOpen() {
// bug:
// http://code.google.com/p/android/issues/detail?id=20545
while (inputStream_.read() != 1) {
trySleep(1000);
trySleep();
}

success = true;
Expand Down Expand Up @@ -268,11 +276,11 @@ private void unregisterReceiver() {
activity_.unregisterReceiver(this);
}

private void trySleep(long time) {
private void trySleep() {
synchronized (AccessoryConnectionBootstrap.this) {
try {
AccessoryConnectionBootstrap.this.wait(time);
} catch (InterruptedException e) {
AccessoryConnectionBootstrap.this.wait(1000);
} catch (InterruptedException ignored) {
}
}
}
Expand All @@ -285,12 +293,12 @@ private class Connection implements IOIOConnection {
private InstanceState instanceState_ = InstanceState.INIT;

@Override
public InputStream getInputStream() throws ConnectionLostException {
public InputStream getInputStream() {
return inputStream_;
}

@Override
public OutputStream getOutputStream() throws ConnectionLostException {
public OutputStream getOutputStream() {
return outputStream_;
}

Expand All @@ -307,7 +315,7 @@ public void waitForConnect() throws ConnectionLostException {
}

try {
AccessoryConnectionBootstrap.this.waitForConnect(this);
AccessoryConnectionBootstrap.this.waitForConnect();
instanceState_ = InstanceState.CONNECTED;
} catch (ConnectionLostException e) {
instanceState_ = InstanceState.DEAD;
Expand All @@ -327,7 +335,7 @@ public void disconnect() {
}

@Override
protected void finalize() throws Throwable {
protected void finalize() {
disconnect();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@

import ioio.lib.spi.NoRuntimeSupportException;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.ParcelFileDescriptor;

/**
Expand All @@ -50,22 +48,22 @@
* UsbAccessory.
*/
class Adapter {
Support support_ = Support.NONE;
Support support_;

Adapter() throws NoRuntimeSupportException {
try {
Class.forName("android.hardware.usb.UsbManager");
support_ = Support.NEW;
return;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException ignored) {
}
try {
Class.forName("android.hardware.usb.UsbManager");
support_ = Support.LEGACY;
return;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException ignored) {
}
throw new NoRuntimeSupportException("No support for USB accesory mode.");
throw new NoRuntimeSupportException("No support for USB accessory mode.");
}

AbstractUsbManager getManager(ContextWrapper wrapper) {
Expand All @@ -79,7 +77,6 @@ AbstractUsbManager getManager(ContextWrapper wrapper) {
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private AbstractUsbManager getManagerNew(ContextWrapper wrapper) {
final android.hardware.usb.UsbManager manager = (android.hardware.usb.UsbManager) wrapper
.getSystemService(Context.USB_SERVICE);
Expand All @@ -92,7 +89,7 @@ private AbstractUsbManager getManagerLegacy(ContextWrapper wrapper) {
}

private enum Support {
NEW, LEGACY, NONE
NEW, LEGACY
}

interface UsbAccessoryInterface {
Expand All @@ -101,8 +98,7 @@ interface UsbAccessoryInterface {
static abstract class AbstractUsbManager {
final String EXTRA_PERMISSION_GRANTED;

protected AbstractUsbManager(String action_usb_accessory_detached,
String extra_permission_granted) {
protected AbstractUsbManager(String extra_permission_granted) {
EXTRA_PERMISSION_GRANTED = extra_permission_granted;
}

Expand All @@ -127,7 +123,7 @@ private static final class LegacyUsbManager extends AbstractUsbManager {
private final android.hardware.usb.UsbManager manager_;

private LegacyUsbManager(android.hardware.usb.UsbManager manager) {
super(android.hardware.usb.UsbManager.ACTION_USB_ACCESSORY_DETACHED,
super(
android.hardware.usb.UsbManager.EXTRA_PERMISSION_GRANTED);
manager_ = manager;
}
Expand Down Expand Up @@ -161,18 +157,17 @@ UsbAccessoryInterface[] getAccessoryList() {
return null;
UsbAccessoryInterface[] result = new UsbAccessoryInterface[accs.length];
for (int i = 0; i < accs.length; ++i) {
result[i] = new UsbAccessoryAdapter<android.hardware.usb.UsbAccessory>(accs[i]);
result[i] = new UsbAccessoryAdapter<>(accs[i]);
}
return result;
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static final class NewUsbManager extends AbstractUsbManager {
private final android.hardware.usb.UsbManager manager_;

private NewUsbManager(android.hardware.usb.UsbManager manager) {
super(android.hardware.usb.UsbManager.ACTION_USB_ACCESSORY_DETACHED,
super(
android.hardware.usb.UsbManager.EXTRA_PERMISSION_GRANTED);
manager_ = manager;
}
Expand Down Expand Up @@ -206,7 +201,7 @@ UsbAccessoryInterface[] getAccessoryList() {
return null;
UsbAccessoryInterface[] result = new UsbAccessoryInterface[accs.length];
for (int i = 0; i < accs.length; ++i) {
result[i] = new UsbAccessoryAdapter<android.hardware.usb.UsbAccessory>(accs[i]);
result[i] = new UsbAccessoryAdapter<>(accs[i]);
}
return result;
}
Expand Down
10 changes: 5 additions & 5 deletions IOIOLibAndroidBluetooth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ dependencies {
api project(":IOIOLibAndroid")
}

project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
publishing {
publications {
release(MavenPublication) {
afterEvaluate {
from components.findByName('release')
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions IOIOLibAndroidDevice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ dependencies {
api project(":IOIOLibAndroid")
}

project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
publishing {
publications {
release(MavenPublication) {
afterEvaluate {
from components.findByName('release')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
* @author Nadir Izrael
* @author Ytai Ben-Tsvi
*/
@TargetApi(12)
public class DeviceConnectionBootstrap extends BroadcastReceiver implements
ContextWrapperDependent, IOIOConnectionBootstrap {
private static final String TAG = "DevConnBootstrap";
Expand All @@ -92,10 +91,8 @@ public class DeviceConnectionBootstrap extends BroadcastReceiver implements
private UsbEndpoint epOut_;
private InputStream inputStream_;
private OutputStream outputStream_;

public DeviceConnectionBootstrap() {
if (Build.VERSION.SDK_INT < 12) {
throw new NoRuntimeSupportException("OTG is not supported on this device.");
}
}

@Override
Expand Down Expand Up @@ -474,8 +471,13 @@ private void checkPermission() {
if (usbManager_.hasPermission(device_)) {
permission_ = Permission.GRANTED;
} else {
pendingIntent_ = PendingIntent.getBroadcast(activity_, 0, new Intent(
ACTION_USB_PERMISSION), 0);
int pendingIntentFlags;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
} else {
pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
}
pendingIntent_ = PendingIntent.getBroadcast(activity_, 0, new Intent(ACTION_USB_PERMISSION), pendingIntentFlags);
usbManager_.requestPermission(device_, pendingIntent_);
permission_ = Permission.PENDING;
}
Expand Down Expand Up @@ -553,12 +555,12 @@ public boolean canClose() {
}

@Override
public InputStream getInputStream() throws ConnectionLostException {
public InputStream getInputStream() {
return inputStream_;
}

@Override
public OutputStream getOutputStream() throws ConnectionLostException {
public OutputStream getOutputStream() {
return outputStream_;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;
Expand Down Expand Up @@ -56,14 +57,21 @@ public int onStartCommand(Intent intent, int flags, int startId) {
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getBaseContext());

int pendingIntentFlags;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
} else {
pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
}

notificationBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("IOIO service running")
.setWhen(System.currentTimeMillis())
.setContentTitle("IOIO Service")
.setContentText("Click to stop")
.setContentIntent(PendingIntent.getService(this, 0, new Intent(
"stop", null, this, this.getClass()), 0));
"stop", null, this, this.getClass()), pendingIntentFlags));

if (notificationManager != null) {
notificationManager.notify(1, notificationBuilder.build());
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'com.android.tools.build:gradle:8.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.hannesa2:gradle-one-jar:1.7"
}
Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ android.useAndroidX=true
android.enableJetifier=true

org.gradle.jvmargs=-Xmx1536m

android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jdk:
- openjdk11
- openjdk17
install:
- ./gradlew :IOIOLibAndroid:build :IOIOLibAndroid:publishToMavenLocal
- ./gradlew :IOIOLibAndroidBluetooth:build :IOIOLibAndroidBluetooth:publishToMavenLocal
Expand Down

0 comments on commit 6e177d1

Please sign in to comment.