Skip to content

Commit

Permalink
Merge pull request #2190 from LiskHQ/fix/android-alarm-settings-issue-v2
Browse files Browse the repository at this point in the history
Fix Android alarm settings issue
  • Loading branch information
shuse2 authored Jan 27, 2025
2 parents 761ee40 + eff7015 commit 693f8cf
Show file tree
Hide file tree
Showing 13 changed files with 1,828 additions and 11,074 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ android {
applicationId "io.lisk.mobile"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 84
versionName "3.0.0"
versionCode 89
versionName "3.0.2"
missingDimensionStrategy 'react-native-camera', 'mlkit'
}

Expand Down
1 change: 0 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />


Expand Down
39 changes: 39 additions & 0 deletions android/app/src/main/java/com/lisk/ExactAlarmModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.lisk.mobile;

import android.app.AlarmManager;
import android.content.Context;
import android.os.Build;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class ExactAlarmModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;

public ExactAlarmModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
public String getName() {
return "ExactAlarmModule";
}

@ReactMethod
public void canScheduleExactAlarms(Promise promise) {
try {
AlarmManager alarmManager = (AlarmManager) reactContext.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
promise.resolve(alarmManager.canScheduleExactAlarms());
} else {
// Before Android 12, exact alarms were always allowed
promise.resolve(true);
}
} catch (Exception e) {
promise.reject("ERROR", e.getMessage());
}
}
}
24 changes: 24 additions & 0 deletions android/app/src/main/java/com/lisk/ExactAlarmPackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.lisk.mobile;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ExactAlarmPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ExactAlarmModule(reactContext));
return modules;
}
}
44 changes: 44 additions & 0 deletions android/app/src/main/java/com/lisk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.defaults.DefaultReactActivityDelegate;
import android.os.Build;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.app.AlarmManager;
import android.content.SharedPreferences;

public class MainActivity extends ReactActivity {

private static final String PREFS_NAME = "AlarmPermissionPrefs";
private static final String KEY_PERMISSION_REQUESTED = "permissionRequested";

/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
Expand All @@ -32,4 +41,39 @@ protected ReactActivityDelegate createReactActivityDelegate() {
DefaultNewArchitectureEntryPoint.getConcurrentReactEnabled() // concurrentRootEnabled
);
}

@Override
protected void onResume() {
super.onResume();
try {
// Use 'this' as context instead of getApplicationContext()
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) return;

SharedPreferences prefs = this.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
if (prefs == null) return;

boolean permissionRequested = prefs.getBoolean(KEY_PERMISSION_REQUESTED, false);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
!alarmManager.canScheduleExactAlarms() &&
!permissionRequested) {
try {
Intent intent = new Intent(android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
intent.setData(Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(KEY_PERMISSION_REQUESTED, true);
editor.apply();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
2 changes: 2 additions & 0 deletions android/app/src/main/java/com/lisk/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.lisk.mobile.RNArgon2Package;
import io.lisk.mobile.UninstallPackage;
import io.lisk.mobile.ScopedStoragePackage;
import io.lisk.mobile.ExactAlarmPackage;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

Expand All @@ -39,6 +40,7 @@ protected List<ReactPackage> getPackages() {
packages.add(new RNArgon2Package());
packages.add(new UninstallPackage());
packages.add(new ScopedStoragePackage());
packages.add(new ExactAlarmPackage());
return packages;
}

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
targetSdkVersion = 33
kotlinVersion = '1.6.0'
// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
ndkVersion = "23.1.7779620"
ndkVersion = "26.1.10909125"
}
repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ MYAPP_RELEASE_KEY_ALIAS=lisk-mobile-alias

compileSdkVersion = 33
buildToolsVersion = "33.0.0"
targetSdkVersion = 33
targetSdkVersion = 34

1 change: 1 addition & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ abstract_target 'LiskApp' do
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.4'
config.build_settings["ONLY_ACTIVE_ARCH"] = "NO"
end
end
__apply_Xcode_12_5_M1_post_install_workaround(installer)
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,6 @@ SPEC CHECKSUMS:
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5

PODFILE CHECKSUM: 48a0b1ff73cf0c2403abad60976fba2e20c845c3
PODFILE CHECKSUM: 2621aca608f1371b714011686c1bbee6b58678a0

COCOAPODS: 1.16.2
Loading

0 comments on commit 693f8cf

Please sign in to comment.