Skip to content

Commit

Permalink
Clean up StarboardBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
haozheng-cobalt committed Jan 3, 2025
1 parent 2d18198 commit 270b2a3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import android.hardware.input.InputManager;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.util.Pair;
import android.util.Size;
import android.util.SizeF;
import android.view.Display;
Expand All @@ -44,11 +40,7 @@
import dev.cobalt.util.Log;
import dev.cobalt.util.UsedByNative;
import java.lang.reflect.Method;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -200,8 +192,6 @@ protected void onServiceDestroy(Service service) {
}
}

@SuppressWarnings("unused")
@UsedByNative
protected void beforeStartOrResume() {
Log.i(TAG, "Prepare to resume");
// Bring our platform services to life before resuming so that they're ready to deal with
Expand All @@ -215,8 +205,6 @@ protected void beforeStartOrResume() {
advertisingId.refresh();
}

@SuppressWarnings("unused")
@UsedByNative
protected void beforeSuspend() {
try {
Log.i(TAG, "Prepare to suspend");
Expand All @@ -233,8 +221,7 @@ protected void beforeSuspend() {
}
}

@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected void afterStopped() {
applicationStopped = true;
ttsHelper.shutdown();
Expand Down Expand Up @@ -266,6 +253,7 @@ protected void applicationStopping() {
applicationStopped = true;
}

// used in starboard/android/shared/system_request_conceal.cc
@SuppressWarnings("unused")
@UsedByNative
public void requestSuspend() {
Expand All @@ -286,6 +274,7 @@ public boolean onSearchRequested() {

// private native boolean nativeOnSearchRequested();

// seems to be used
@SuppressWarnings("unused")
@UsedByNative
public Context getApplicationContext() {
Expand All @@ -295,6 +284,7 @@ public Context getApplicationContext() {
return appContext;
}

// starboard/android/shared/system_platform_error.cc
@SuppressWarnings("unused")
@UsedByNative
void raisePlatformError(@PlatformError.ErrorType int errorType, long data) {
Expand All @@ -315,8 +305,7 @@ protected Holder<Activity> getActivityHolder() {
return activityHolder;
}

@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected String[] getArgs() {
if (args == null) {
throw new IllegalArgumentException("args cannot be null");
Expand All @@ -325,8 +314,7 @@ protected String[] getArgs() {
}

/** Returns the URL from the Intent that started the app. */
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected String getStartDeepLink() {
if (startDeepLink == null) {
throw new IllegalArgumentException("startDeepLink cannot be null");
Expand All @@ -349,6 +337,7 @@ private void nativeHandleDeepLink(String url) {
// TODO(b/374147993): Implement deep link
}

// starboard/android/shared/file_internal.cc
/**
* Returns the absolute path to the directory where application specific files should be written.
* May be overridden for use cases that need to segregate storage.
Expand All @@ -359,6 +348,7 @@ protected String getFilesAbsolutePath() {
return appContext.getFilesDir().getAbsolutePath();
}

// starboard/android/shared/file_internal.cc
/**
* Returns the absolute path to the application specific cache directory on the filesystem. May be
* overridden for use cases that need to segregate storage.
Expand All @@ -369,62 +359,7 @@ protected String getCacheAbsolutePath() {
return appContext.getCacheDir().getAbsolutePath();
}

/**
* Returns non-loopback network interface address and its netmask, or null if none.
*
* <p>A Java function to help implement Starboard's SbSocketGetLocalInterfaceAddress.
*/
@SuppressWarnings("unused")
@UsedByNative
// TODO: (cobalt b/372559388) Migrate complicated returned type functions to JNI zero.
// The @UsedByNative annotation has strict signature parsing rules,
// and Pair<byte[], byte[]> is not be supported well.
Pair<byte[], byte[]> getLocalInterfaceAddressAndNetmask(boolean wantIPv6) {
try {
Enumeration<NetworkInterface> it = NetworkInterface.getNetworkInterfaces();

while (it.hasMoreElements()) {
NetworkInterface ni = it.nextElement();
if (ni.isLoopback()) {
continue;
}
if (!ni.isUp()) {
continue;
}
if (ni.isPointToPoint()) {
continue;
}

for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
byte[] address = ia.getAddress().getAddress();
boolean isIPv6 = (address.length > 4);
if (isIPv6 == wantIPv6) {
// Convert the network prefix length to a network mask.
int prefix = ia.getNetworkPrefixLength();
byte[] netmask = new byte[address.length];
for (int i = 0; i < netmask.length; i++) {
if (prefix == 0) {
netmask[i] = 0;
} else if (prefix >= 8) {
netmask[i] = (byte) 0xFF;
prefix -= 8;
} else {
netmask[i] = (byte) (0xFF << (8 - prefix));
prefix = 0;
}
}
return new Pair<>(address, netmask);
}
}
}
} catch (SocketException ex) {
// TODO should we have a logging story that strips logs for production?
Log.w(TAG, "sbSocketGetLocalInterfaceAddress exception", ex);
return null;
}
return null;
}

// starboard/android/shared/speech_synthesis_speak.cc
@SuppressWarnings("unused")
@UsedByNative
CobaltTextToSpeechHelper getTextToSpeechHelper() {
Expand All @@ -434,6 +369,7 @@ CobaltTextToSpeechHelper getTextToSpeechHelper() {
return ttsHelper;
}

// starboard/android/shared/accessibility_get_caption_settings.cc
/**
* @return A new CaptionSettings object with the current system caption settings.
*/
Expand All @@ -445,13 +381,15 @@ CaptionSettings getCaptionSettings() {
return new CaptionSettings(cm);
}

// starboard/android/shared/system_get_locale_id.cc
/** Java-layer implementation of SbSystemGetLocaleId. */
@SuppressWarnings("unused")
@UsedByNative
String systemGetLocaleId() {
return Locale.getDefault().toLanguageTag();
}

// starboard/android/shared/time_zone_get_name.cc
@SuppressWarnings("unused")
@UsedByNative
String getTimeZoneId() {
Expand All @@ -464,18 +402,18 @@ String getTimeZoneId() {
return timeZone.getID();
}

// starboard/android/shared/window_get_diagonal_size_in_inches.cc
@SuppressWarnings("unused")
@UsedByNative
SizeF getDisplayDpi() {
return DisplayUtil.getDisplayDpi();
}

@SuppressWarnings("unused")
@UsedByNative
Size getDisplaySize() {
return DisplayUtil.getSystemDisplaySize();
}

// need to migrate
@SuppressWarnings("unused")
@UsedByNative
public ResourceOverlay getResourceOverlay() {
Expand All @@ -498,6 +436,7 @@ private static String getSystemProperty(String name) {
}
}

// starboard/android/shared/window_get_size.cc
@SuppressWarnings("unused")
@UsedByNative
Size getDeviceResolution() {
Expand All @@ -522,6 +461,7 @@ Size getDeviceResolution() {
}
}

// starboard/android/shared/system_network_is_disconnected.cc
@SuppressWarnings("unused")
@UsedByNative
boolean isNetworkConnected() {
Expand All @@ -531,6 +471,7 @@ boolean isNetworkConnected() {
return networkStatus.isConnected();
}

// starboard/android/shared/microphone_impl.cc
/**
* Checks if there is no microphone connected to the system.
*
Expand Down Expand Up @@ -560,6 +501,7 @@ public boolean isMicrophoneDisconnected() {
return true;
}

// starboard/android/shared/microphone_impl.cc
/**
* Checks if the microphone is muted.
*
Expand All @@ -572,26 +514,7 @@ public boolean isMicrophoneMute() {
return audioManager.isMicrophoneMute();
}

/**
* @return true if we have an active network connection and it's on an wireless network.
*/
@SuppressWarnings("unused")
@UsedByNative
boolean isCurrentNetworkWireless() {
ConnectivityManager connMgr =
(ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork = connMgr.getActiveNetwork();
if (activeNetwork == null) {
return false;
}
NetworkCapabilities activeCapabilities = connMgr.getNetworkCapabilities(activeNetwork);
if (activeCapabilities == null) {
return false;
}
// Consider anything that's not definitely wired to be wireless.
return !activeCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
}

// starboard/android/shared/accessibility_get_display_settings.cc
/**
* @return true if the user has enabled accessibility high contrast text in the operating system.
*/
Expand All @@ -610,6 +533,7 @@ boolean isAccessibilityHighContrastTextEnabled() {
}
}

// starboard/android/shared/android_media_session_client.cc
@SuppressWarnings("unused")
@UsedByNative
void updateMediaSession(
Expand All @@ -629,6 +553,7 @@ void updateMediaSession(
// playbackState, actions, positionMs, speed, title, artist, album, artwork, duration);
}

// starboard/android/shared/android_media_session_client.cc
@SuppressWarnings("unused")
@UsedByNative
public void deactivateMediaSession() {
Expand All @@ -637,6 +562,7 @@ public void deactivateMediaSession() {
// cobaltMediaSession.deactivateMediaSession();
}

// starboard/android/shared/system_get_property.cc
/** Returns string for kSbSystemPropertyUserAgentAuxField */
@SuppressWarnings("unused")
@UsedByNative
Expand Down Expand Up @@ -665,20 +591,23 @@ protected String getUserAgentAuxField() {
return sb.toString();
}

// starboard/android/shared/system_get_property.cc
/** Returns string for kSbSystemPropertyAdvertisingId */
@SuppressWarnings("unused")
@UsedByNative
protected String getAdvertisingId() {
return this.advertisingId.getId();
}

// starboard/android/shared/system_get_property.cc
/** Returns boolean for kSbSystemPropertyLimitAdTracking */
@SuppressWarnings("unused")
@UsedByNative
protected boolean getLimitAdTracking() {
return this.advertisingId.isLimitAdTrackingEnabled();
}

// starboard/android/shared/audio_track_bridge.cc
@SuppressWarnings("unused")
@UsedByNative
AudioOutputManager getAudioOutputManager() {
Expand All @@ -699,6 +628,7 @@ AudioOutputManager getAudioOutputManager() {
// audioPermissionRequester.onRequestPermissionsResult(requestCode, permissions, grantResults);
// }

// starboard/android/shared/video_window.cc
@SuppressWarnings("unused")
@UsedByNative
public void resetVideoSurface() {
Expand All @@ -708,6 +638,7 @@ public void resetVideoSurface() {
}
}

// starboard/android/shared/player_set_bounds.cc
@SuppressWarnings("unused")
@UsedByNative
public void setVideoSurfaceBounds(final int x, final int y, final int width, final int height) {
Expand All @@ -717,6 +648,7 @@ public void setVideoSurfaceBounds(final int x, final int y, final int width, fin
}
}

// starboard/android/shared/media_capabilities_cache.cc
/** Return supported hdr types. */
@SuppressWarnings("unused")
@UsedByNative
Expand Down Expand Up @@ -795,6 +727,7 @@ public byte[] sendToCobaltService(String serviceName, byte [] data) {
return response.data;
}

// ?
/** Returns the application start timestamp. */
@SuppressWarnings("unused")
@CalledByNative
Expand All @@ -810,6 +743,7 @@ protected long getAppStartTimestamp() {
return 0;
}

// starboard/android/shared/graphics.cc
@SuppressWarnings("unused")
@UsedByNative
void reportFullyDrawn() {
Expand All @@ -819,6 +753,7 @@ void reportFullyDrawn() {
}
}

// starboard/android/shared/crash_handler.cc
@SuppressWarnings("unused")
@UsedByNative
public void setCrashContext(String key, String value) {
Expand All @@ -838,19 +773,19 @@ public void registerCrashContextUpdateHandler(CrashContextUpdateHandler handler)
}

@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected boolean getIsAmatiDevice() {
return this.isAmatiDevice;
}

@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected String getBuildFingerprint() {
return Build.FINGERPRINT;
}

@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected long getPlayServicesVersion() {
try {
if (android.os.Build.VERSION.SDK_INT < 28) {
Expand Down
Loading

0 comments on commit 270b2a3

Please sign in to comment.