Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS compatibility improvements #1121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ android {
}

dependencies {
/** AndroidX **/
implementation 'androidx.core:core:1.8.0'

/** Third-party **/
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.70'
implementation 'org.jcodec:jcodec:0.2.3'
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/limelight/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -275,13 +277,13 @@ public boolean onCapturedPointer(View view, MotionEvent motionEvent) {
}

// Warn the user if they're on a metered connection
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connMgr = ContextCompat.getSystemService(this, ConnectivityManager.class);
if (connMgr.isActiveNetworkMetered()) {
displayTransientMessage(getResources().getString(R.string.conn_metered));
}

// Make sure Wi-Fi is fully powered up
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager wifiMgr = ContextCompat.getSystemService(getApplicationContext(), WifiManager.class);
try {
highPerfWifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Moonlight High Perf Lock");
highPerfWifiLock.setReferenceCounted(false);
Expand Down Expand Up @@ -472,7 +474,7 @@ public void notifyCrash(Exception e) {
controllerHandler = new ControllerHandler(this, conn, this, prefConfig);
keyboardTranslator = new KeyboardTranslator();

InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
InputManager inputManager = ContextCompat.getSystemService(this, InputManager.class);
inputManager.registerInputDeviceListener(controllerHandler, null);
inputManager.registerInputDeviceListener(keyboardTranslator, null);

Expand Down Expand Up @@ -1024,7 +1026,7 @@ public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
protected void onDestroy() {
super.onDestroy();

InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
InputManager inputManager = ContextCompat.getSystemService(this, InputManager.class);
if (controllerHandler != null) {
inputManager.unregisterInputDeviceListener(controllerHandler);
}
Expand Down Expand Up @@ -1357,7 +1359,7 @@ private TouchContext getTouchContext(int actionIndex)
@Override
public void toggleKeyboard() {
LimeLog.info("Toggling keyboard overlay");
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager inputManager = ContextCompat.getSystemService(this, InputMethodManager.class);
inputManager.toggleSoftInput(0, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ private AudioTrack createAudioTrack(int channelConfig, int sampleRate, int buffe
.setChannelMask(channelConfig)
.build();

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// Use FLAG_LOW_LATENCY on L through N
if (lowLatency) {
attributesBuilder.setFlags(AudioAttributes.FLAG_LOW_LATENCY);
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AudioTrack.Builder trackBuilder = new AudioTrack.Builder()
.setAudioFormat(format)
.setAudioAttributes(attributesBuilder.build())
.setTransferMode(AudioTrack.MODE_STREAM)
.setBufferSizeInBytes(bufferSize);

// Use PERFORMANCE_MODE_LOW_LATENCY on O and later
if (lowLatency) {
trackBuilder.setPerformanceMode(AudioTrack.PERFORMANCE_MODE_LOW_LATENCY);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O
) {
// Use FLAG_LOW_LATENCY on N through N_MR1
attributesBuilder.setFlags(AudioAttributes.FLAG_LOW_LATENCY);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Use PERFORMANCE_MODE_LOW_LATENCY on O and later
trackBuilder.setPerformanceMode(AudioTrack.PERFORMANCE_MODE_LOW_LATENCY);
}
}

trackBuilder.setAudioAttributes(attributesBuilder.build());
return trackBuilder.build();
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.limelight.LimeLog;
import com.limelight.binding.input.driver.AbstractController;
import com.limelight.binding.input.driver.UsbDriverListener;
Expand Down Expand Up @@ -70,7 +72,7 @@ public ControllerHandler(Activity activityContext, NvConnection conn, GameGestur
this.conn = conn;
this.gestures = gestures;
this.prefConfig = prefConfig;
this.deviceVibrator = (Vibrator) activityContext.getSystemService(Context.VIBRATOR_SERVICE);
this.deviceVibrator = ContextCompat.getSystemService(activityContext, Vibrator.class);

this.sceManager = new SceManager(activityContext);
this.sceManager.start();
Expand Down Expand Up @@ -257,7 +259,7 @@ public static short getAttachedControllerMask(Context context) {
short mask = 0;

// Count all input devices that are gamepads
InputManager im = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
InputManager im = ContextCompat.getSystemService(context, InputManager.class);
for (int id : im.getInputDeviceIds()) {
InputDevice dev = im.getInputDevice(id);
if (dev == null) {
Expand All @@ -272,7 +274,7 @@ public static short getAttachedControllerMask(Context context) {

// Count all USB devices that match our drivers
if (PreferenceConfiguration.readPreferences(context).usbDriver) {
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
UsbManager usbManager = ContextCompat.getSystemService(context, UsbManager.class);
for (UsbDevice dev : usbManager.getDeviceList().values()) {
// We explicitly check not to claim devices that appear as InputDevices
// otherwise we will double count them.
Expand Down Expand Up @@ -457,7 +459,7 @@ private boolean shouldIgnoreBack(InputDevice dev) {
//
// First, check if this is an internal device we're being called on.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && !isExternal(dev)) {
InputManager im = (InputManager) activityContext.getSystemService(Context.INPUT_SERVICE);
InputManager im = ContextCompat.getSystemService(activityContext, InputManager.class);

boolean foundInternalGamepad = false;
boolean foundInternalSelect = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.hardware.input.InputManager;
import android.view.MotionEvent;

import androidx.core.content.ContextCompat;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -50,7 +52,8 @@ public static boolean isCaptureProviderSupported() {

private boolean setCursorVisibility(boolean visible) {
try {
methodSetCursorVisibility.invoke(context.getSystemService(Context.INPUT_SERVICE), visible);
methodSetCursorVisibility.invoke(
ContextCompat.getSystemService(context, InputManager.class), visible);
return true;
} catch (InvocationTargetException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.view.InputDevice;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.limelight.LimeLog;
import com.limelight.R;
import com.limelight.preferences.PreferenceConfiguration;
Expand Down Expand Up @@ -299,7 +301,7 @@ private void stop() {

@Override
public void onCreate() {
this.usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
this.usbManager = ContextCompat.getSystemService(this, UsbManager.class);
this.prefConfig = PreferenceConfiguration.readPreferences(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
Expand Down Expand Up @@ -160,7 +162,7 @@ protected void onElementDraw(Canvas canvas) {
canvas.drawOval(rect, paint);

if (icon != -1) {
Drawable d = getResources().getDrawable(icon);
Drawable d = ContextCompat.getDrawable(getContext(), icon);
d.setBounds(5, 5, getWidth() - 5, getHeight() - 5);
d.draw(canvas);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.media.MediaFormat;
import android.os.Build;

import androidx.core.content.ContextCompat;

import com.limelight.LimeLog;
import com.limelight.preferences.PreferenceConfiguration;

Expand Down Expand Up @@ -314,7 +316,7 @@ public static void initialize(Context context, String glRenderer) {
}

ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ContextCompat.getSystemService(context, ActivityManager.class);
ConfigurationInfo configInfo = activityManager.getDeviceConfigurationInfo();
if (configInfo.reqGlEsVersion != ConfigurationInfo.GL_ES_VERSION_UNDEFINED) {
LimeLog.info("OpenGL ES version: "+configInfo.reqGlEsVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
Expand All @@ -40,6 +39,8 @@
import android.os.IBinder;
import android.os.SystemClock;

import androidx.core.content.ContextCompat;

import org.xmlpull.v1.XmlPullParserException;

public class ComputerManagerService extends Service {
Expand Down Expand Up @@ -331,7 +332,7 @@ public boolean onUnbind(Intent intent) {
private void populateExternalAddress(ComputerDetails details) {
boolean boundToNetwork = false;
boolean activeNetworkIsVpn = NetHelper.isActiveNetworkVpn(this);
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connMgr = ContextCompat.getSystemService(this, ConnectivityManager.class);

// Check if we're currently connected to a VPN which may send our
// STUN request from an unexpected interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import com.limelight.nvstream.mdns.MdnsDiscoveryListener;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Binder;
import android.os.IBinder;

import androidx.core.content.ContextCompat;

public class DiscoveryService extends Service {

private MdnsDiscoveryAgent discoveryAgent;
Expand Down Expand Up @@ -42,7 +43,7 @@ public List<MdnsComputer> getComputerSet() {

@Override
public void onCreate() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager wifiMgr = ContextCompat.getSystemService(getApplicationContext(), WifiManager.class);
multicastLock = wifiMgr.createMulticastLock("Limelight mDNS");
multicastLock.setReferenceCounted(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class GenericGridAdapter<T> extends BaseAdapter {
this.context = context;
this.layoutId = layoutId;

this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.inflater = LayoutInflater.from(context);
}

void setLayoutId(int layoutId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.graphics.ImageDecoder;
import android.os.Build;

import androidx.core.content.ContextCompat;

import com.limelight.LimeLog;
import com.limelight.utils.CacheHelper;

Expand All @@ -30,7 +32,7 @@ public DiskAssetLoader(Context context) {
this.cacheDir = context.getCacheDir();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.isLowRamDevice =
((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).isLowRamDevice();
ContextCompat.getSystemService(context, ActivityManager.class).isLowRamDevice();
}
else {
// Use conservative low RAM behavior on very old devices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.util.LruCache;

import androidx.core.graphics.BitmapCompat;

import com.limelight.LimeLog;

import java.lang.ref.SoftReference;
Expand All @@ -13,7 +15,7 @@ public class MemoryAssetLoader {
@Override
protected int sizeOf(String key, ScaledBitmap bitmap) {
// Sizeof returns kilobytes
return bitmap.bitmap.getByteCount() / 1024;
return BitmapCompat.getAllocationByteCount(bitmap.bitmap) / 1024;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
Expand All @@ -36,6 +35,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

public class AddComputerManually extends Activity {
private TextView hostText;
private ComputerManagerService.ComputerManagerBinder managerBinder;
Expand Down Expand Up @@ -241,7 +242,8 @@ public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent
}
else if (actionId == EditorInfo.IME_ACTION_PREVIOUS) {
// This is how the Fire TV dismisses the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager imm = ContextCompat.getSystemService(
AddComputerManually.this, InputMethodManager.class);
imm.hideSoftInputFromWindow(hostText.getWindowToken(), 0);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.limelight.preferences;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand All @@ -26,6 +25,8 @@
import android.view.ViewGroup;
import android.view.WindowInsets;

import androidx.core.content.ContextCompat;

import com.limelight.LimeLog;
import com.limelight.PcView;
import com.limelight.R;
Expand Down Expand Up @@ -270,7 +271,7 @@ public void onCreate(Bundle savedInstanceState) {
}*/

// Remove the vibration options if the device can't vibrate
if (!((Vibrator)getActivity().getSystemService(Context.VIBRATOR_SERVICE)).hasVibrator()) {
if (!ContextCompat.getSystemService(getActivity(), Vibrator.class).hasVibrator()) {
PreferenceCategory category =
(PreferenceCategory) findPreference("category_input_settings");
category.removePreference(findPreference("checkbox_vibrate_fallback"));
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/limelight/utils/NetHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import android.net.NetworkInfo;
import android.os.Build;

import androidx.core.content.ContextCompat;

public class NetHelper {
public static boolean isActiveNetworkVpn(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connMgr = ContextCompat.getSystemService(context, ConnectivityManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network activeNetwork = connMgr.getActiveNetwork();
if (activeNetwork != null) {
Expand Down
Loading