-
Notifications
You must be signed in to change notification settings - Fork 171
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
Bugfix/issue 1812 Android 13 Support #1826
Changes from 4 commits
8dd082d
70754e7
7ba0d67
399f373
bf7e660
cb1c48e
881bbc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,18 +1,17 @@ | ||||||||
package com.sdl.hellosdlandroid; | ||||||||
|
||||||||
import android.Manifest; | ||||||||
import android.content.Intent; | ||||||||
import android.content.pm.PackageManager; | ||||||||
import android.os.Build; | ||||||||
import android.os.Bundle; | ||||||||
import android.view.Menu; | ||||||||
import android.view.MenuItem; | ||||||||
|
||||||||
import androidx.annotation.NonNull; | ||||||||
import androidx.appcompat.app.AppCompatActivity; | ||||||||
import androidx.core.app.ActivityCompat; | ||||||||
import androidx.core.content.ContextCompat; | ||||||||
|
||||||||
import static android.Manifest.permission.BLUETOOTH_CONNECT; | ||||||||
import java.util.ArrayList; | ||||||||
|
||||||||
public class MainActivity extends AppCompatActivity { | ||||||||
|
||||||||
|
@@ -23,12 +22,14 @@ protected void onCreate(Bundle savedInstanceState) { | |||||||
super.onCreate(savedInstanceState); | ||||||||
setContentView(R.layout.activity_main); | ||||||||
|
||||||||
|
||||||||
if (BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) { | ||||||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !checkPermission()) { | ||||||||
requestPermission(); | ||||||||
return; | ||||||||
if (permissionsNeeded().length > 0) { | ||||||||
requestPermission(permissionsNeeded(), REQUEST_CODE); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if (checkBTPermission()) { | ||||||||
return; | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this is still needed? Seems like we could get rid of it.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed because we need to not call We could change it below at line 35 to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With having an array with strings that contain our permission needs already it seems like we could check those with something like: for (String permission : permissionsNeeded) {
if(Manifest.permission.BLUETOOTH_CONNECT.equals(permission)){
return;
}
} I just don't know the efficiency of checking for the permission again, and it seems redundant. At the very least I would say your suggestions should be used as it's bit more concise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option would be to flip the methods for checking permission into returning if the permission is granted instead of currently returning if it is not granted, because that in itself is a big confusing as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it was confusing especially with the naming of those methods, I flipped them and renamed to |
||||||||
} | ||||||||
|
||||||||
//If we are connected to a module we want to start our SdlService | ||||||||
SdlReceiver.queryForConnectedService(this); | ||||||||
} else if (BuildConfig.TRANSPORT.equals("TCP")){ | ||||||||
|
@@ -37,24 +38,53 @@ protected void onCreate(Bundle savedInstanceState) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
private boolean checkPermission() { | ||||||||
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT); | ||||||||
private boolean checkBTPermission() { | ||||||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !checkPermission(Manifest.permission.BLUETOOTH_CONNECT); | ||||||||
} | ||||||||
|
||||||||
private boolean checkPNPermission() { | ||||||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !checkPermission(Manifest.permission.POST_NOTIFICATIONS); | ||||||||
} | ||||||||
|
||||||||
private void requestPermission() { | ||||||||
ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT}, REQUEST_CODE); | ||||||||
private boolean checkPermission(String permission) { | ||||||||
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), permission); | ||||||||
} | ||||||||
|
||||||||
private void requestPermission(String[] permissions, int REQUEST_CODE) { | ||||||||
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE); | ||||||||
} | ||||||||
|
||||||||
private String[] permissionsNeeded() { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't a big deal, but still a good idea.
Suggested change
|
||||||||
ArrayList<String> result = new ArrayList<>(); | ||||||||
if (checkBTPermission()) { | ||||||||
result.add(Manifest.permission.BLUETOOTH_CONNECT); | ||||||||
} | ||||||||
if (checkPNPermission()) { | ||||||||
result.add(Manifest.permission.POST_NOTIFICATIONS); | ||||||||
} | ||||||||
return (result.toArray(new String[result.size()])); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||||||||
switch (requestCode) { | ||||||||
case REQUEST_CODE: | ||||||||
if (grantResults.length > 0) { | ||||||||
|
||||||||
boolean btConnectGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED; | ||||||||
|
||||||||
if (btConnectGranted) { | ||||||||
SdlReceiver.queryForConnectedService(this); | ||||||||
for (int i = 0; i < grantResults.length; i++) { | ||||||||
if (permissions[i].equals(Manifest.permission.BLUETOOTH_CONNECT)) { | ||||||||
boolean btConnectGranted = | ||||||||
grantResults[i] == PackageManager.PERMISSION_GRANTED; | ||||||||
if (btConnectGranted) { | ||||||||
SdlReceiver.queryForConnectedService(this); | ||||||||
} | ||||||||
} else if (permissions[i].equals(Manifest.permission.POST_NOTIFICATIONS)) { | ||||||||
boolean postNotificationGranted = | ||||||||
grantResults[i] == PackageManager.PERMISSION_GRANTED; | ||||||||
if (!postNotificationGranted) { | ||||||||
// User denied permission, Notifications for SDL will not appear | ||||||||
// on Android 13 devices. | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
break; | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.