Skip to content

Commit

Permalink
BT update & fixes ...
Browse files Browse the repository at this point in the history
  • Loading branch information
fesch committed Aug 18, 2023
1 parent 18f430d commit b04ee5a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 77 deletions.
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ android {
keyPassword 'canze4us!'
}
}
compileSdkVersion 33
//buildToolsVersion '26.0.2'
compileSdk 34

defaultConfig {
applicationId "lu.fisch.canze"
minSdkVersion 21
versionName '1.64'
versionCode 96
versionName '1.65'
versionCode 98
}
buildTypes {
release {
minifyEnabled false
// minifyEnabled true
//minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
//debug {
Expand All @@ -41,7 +41,7 @@ android {
}
defaultConfig {

targetSdk 33
targetSdk 34
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "String", "BRANCH", "\"" + getGitRevParseInfo() + "\""
signingConfig signingConfigs.release
Expand All @@ -65,8 +65,8 @@ dependencies {
playstoreImplementation platform('com.google.firebase:firebase-bom:32.2.2')

// Dependencies for the desired Firebase products without specifying versions
playstoreImplementation 'com.google.firebase:firebase-analytics'
playstoreImplementation 'com.google.firebase:firebase-crashlytics'
playstoreImplementation 'com.google.firebase:firebase-analytics:21.3.0'
playstoreImplementation 'com.google.firebase:firebase-crashlytics:18.4.0'

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--<activity
android:name=".activities.SplashActivity"
android:label="@string/app_name" >
</activity>-->
<activity
android:name=".activities.SettingsActivity"
android:label="@string/title_activity_settings" />
Expand Down
177 changes: 109 additions & 68 deletions app/src/main/java/lu/fisch/canze/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,10 @@ public class MainActivity extends AppCompatActivity implements FieldListener /*,
private int mBtState = BLUETOOTH_SEARCH;

private boolean storageGranted = false;
private boolean btConnectGranted = false;
private boolean btLocationGranted = false;
private int startAnotherFragmentIndex = -2;

private static String[] PERMISSIONS_BLUETOOTH = {
// Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT
//Manifest.permission.BLUETOOTH_PRIVILEGED
};

private void checkPermissions(){
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT);
if (permission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(
this,
PERMISSIONS_BLUETOOTH,
1
);
}
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -297,7 +282,7 @@ public static MainActivity getInstance() {
public static void debug(String text) {
if (text == null) text = "null";
//if (!BuildConfig.BRANCH.equals("master")) {
Log.d(TAG, text);
Log.d(TAG, text);
//}
if (storageIsAvailable && debugLogMode) {
DebugLogger.getInstance().log(text);
Expand Down Expand Up @@ -486,31 +471,88 @@ private void updateActionBar() {
}
}

private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1234;

private ViewPager viewPager;
private ActionBar actionBar;

private static final int MY_PERMISSIONS_REQUEST = 123;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
case MY_PERMISSIONS_REQUEST:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
MainActivity.debug("MainActivity.onRequestPermissionsResult:storage granted");
MainActivity.debug("MainActivity.onRequestPermissionsResult:permissions granted");
storageGranted = true;
btConnectGranted = true;
btLocationGranted = true;

// configure Bluetooth manager
BluetoothManager.getInstance().setBluetoothEvent(new BluetoothEvent() {
@Override
public void onBeforeConnect() {
showBluetoothState(BLUETOOTH_SEARCH);
}

@Override
public void onAfterConnect(BluetoothSocket bluetoothSocket) {
if (device != null)
device.init(visible);
showBluetoothState(BLUETOOTH_CONNECTED);
}

@Override
public void onBeforeDisconnect(BluetoothSocket bluetoothSocket) {
}

@Override
public void onAfterDisconnect() {
showBluetoothState(BLUETOOTH_DISCONNECTED);
}
});
// detect hardware status
int BT_STATE = BluetoothManager.getInstance().getHardwareState();
if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_AVAILABLE)
toast("Sorry, but your device doesn't seem to have Bluetooth support!");
else if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_ACTIVE) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}

} else {
storageGranted = false;
MainActivity.debug("MainActivity.onRequestPermissionsResult:storage not granted");
btConnectGranted = false;
btLocationGranted = false;
MainActivity.debug("MainActivity.onRequestPermissionsResult:permissions not granted");
}
break;

// other 'case' lines to check for other
// permissions this app might request.


default:
break;
}
}

private ViewPager viewPager;
private ActionBar actionBar;
public void checkPermissions()
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
||
ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED
||
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_FINE_LOCATION
}, MY_PERMISSIONS_REQUEST);
} else {
storageGranted=true;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -520,14 +562,6 @@ protected void onCreate(Bundle savedInstanceState) {
// needed to get strings from resources in non-Activity classes
res = getResources();

if (ContextCompat.checkSelfPermission(instance, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(instance, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
storageGranted = true;
}

checkPermissions();

handleDarkMode();

// dataLogger = DataLogger.getInstance();
Expand All @@ -541,7 +575,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// navigation bar
AppSectionsPagerAdapter appSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar = getSupportActionBar();
Expand All @@ -560,6 +593,32 @@ public void onPageSelected(int position) {
});
updateActionBar();

/*
int permission;
permission= ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT);
Log.w("canze-bt","BLUETOOTH_CONNECT: "+permission);
if (permission != PackageManager.PERMISSION_GRANTED){
Log.w("canze-bt","BLUETOOTH_CONNECT: request!");
ActivityCompat.requestPermissions(
this,
new String[] {Manifest.permission.BLUETOOTH_CONNECT},
REQ
);
}
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
Log.w("canze-bt","ACCESS_FINE_LOCATION: "+permission);
Log.w("canze-bt","PERMISSION_GRANTED: "+PackageManager.PERMISSION_GRANTED);
if (permission != PackageManager.PERMISSION_GRANTED){
Log.w("canze-bt","ACCESS_FINE_LOCATION: request!");
ActivityCompat.requestPermissions(
this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
1
);
}*/

// setTitle(TAG + " - not connected");
showBluetoothState(BLUETOOTH_DISCONNECTED);

Expand All @@ -584,37 +643,7 @@ public void onPageSelected(int position) {
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(broadcastReceiver, intentFilter);

// configure Bluetooth manager
BluetoothManager.getInstance().setBluetoothEvent(new BluetoothEvent() {
@Override
public void onBeforeConnect() {
showBluetoothState(BLUETOOTH_SEARCH);
}

@Override
public void onAfterConnect(BluetoothSocket bluetoothSocket) {
if (device != null)
device.init(visible);
showBluetoothState(BLUETOOTH_CONNECTED);
}

@Override
public void onBeforeDisconnect(BluetoothSocket bluetoothSocket) {
}

@Override
public void onAfterDisconnect() {
showBluetoothState(BLUETOOTH_DISCONNECTED);
}
});
// detect hardware status
int BT_STATE = BluetoothManager.getInstance().getHardwareState();
if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_AVAILABLE)
toast("Sorry, but your device doesn't seem to have Bluetooth support!");
else if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_ACTIVE) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}

// load settings
// - includes the reader
Expand All @@ -637,6 +666,8 @@ else if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_ACTIVE) {
}
debug("Loading fields last field values from database (done)");
})).start();

checkPermissions();
}

void handleDarkMode()
Expand Down Expand Up @@ -670,6 +701,7 @@ void handleDarkMode()
}
}

private boolean displayDone = false;

@Override
public void onResume() {
Expand Down Expand Up @@ -708,8 +740,10 @@ public void onResume() {
(new Thread(this::reloadBluetooth)).start();
}

if(!displayDone)
if (!this.settings.getBoolean(SettingsActivity.SETTING_APP_DISCLAIMER_SEEN, false)) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
displayDone=true;

// set title
alertDialogBuilder.setTitle(R.string.prompt_Disclaimer);
Expand Down Expand Up @@ -997,9 +1031,9 @@ private void showBluetoothState(final int btState) {
imageView.setBackgroundResource(R.drawable.bluetooth_3); // 23, blue icon
break;
case BLUETOOTH_SEARCH:
imageView.setBackgroundResource(R.drawable.animation_bluetooth); // 22, animated blue icon
// AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if (frameAnimation != null) frameAnimation.start();
imageView.setBackgroundResource(R.drawable.animation_bluetooth); // 22, animated blue icon
// AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if (frameAnimation != null) frameAnimation.start();
break;
default:
break;
Expand Down Expand Up @@ -1041,8 +1075,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

// load the activity
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivityForResult(intent, SETTINGS_ACTIVITY);
//checkPermissions();
if(ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)
{
toast("Can't open settings without having the permission to use bluetooth. Sorry!");
}
else {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivityForResult(intent, SETTINGS_ACTIVITY);
}
})).start();
return true;
}
Expand Down

0 comments on commit b04ee5a

Please sign in to comment.