Skip to content

Commit

Permalink
Merge pull request #22 from akaMrNagar/dev
Browse files Browse the repository at this point in the history
Feat: Notification batching
  • Loading branch information
akaMrNagar authored Dec 30, 2024
2 parents b70c3b8 + 53dd9cc commit d018e86
Show file tree
Hide file tree
Showing 117 changed files with 4,266 additions and 1,215 deletions.
16 changes: 16 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
tools:ignore="ProtectedPermissions" />

<application
android:name="${applicationName}"
Expand Down Expand Up @@ -115,6 +118,10 @@
android:name=".receivers.alarm.MidnightResetReceiver"
android:enabled="true"
android:exported="false" />
<receiver
android:name=".receivers.alarm.NotificationBatchReceiver"
android:enabled="true"
android:exported="false" />
<!-- _________________________________________________________________________________ -->
<!-- ________________________________ SERVICES _______________________________________ -->
<!-- _________________________________________________________________________________ -->
Expand Down Expand Up @@ -169,6 +176,15 @@
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
android:value="false" />
</service>
<service
android:name=".services.MindfulNotificationListenerService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
<!-- Don't delete the meta-data below.This
is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
91 changes: 63 additions & 28 deletions android/app/src/main/java/com/mindful/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
package com.mindful.android;

import static com.mindful.android.generics.ServiceBinder.ACTION_START_MINDFUL_SERVICE;
import static com.mindful.android.helpers.NewActivitiesLaunchHelper.INTENT_EXTRA_IS_SELF_RESTART;
import static com.mindful.android.services.OverlayDialogService.INTENT_EXTRA_PACKAGE_NAME;
import static com.mindful.android.utils.AppConstants.INTENT_EXTRA_INITIAL_ROUTE;
import static com.mindful.android.utils.AppConstants.INTENT_EXTRA_IS_SELF_RESTART;
import static com.mindful.android.utils.AppConstants.INTENT_EXTRA_PACKAGE_NAME;

import android.content.Intent;
import android.content.res.Configuration;
Expand All @@ -38,6 +39,7 @@
import com.mindful.android.models.RestrictionGroup;
import com.mindful.android.services.EmergencyPauseService;
import com.mindful.android.services.FocusSessionService;
import com.mindful.android.services.MindfulNotificationListenerService;
import com.mindful.android.services.MindfulTrackerService;
import com.mindful.android.services.MindfulVpnService;
import com.mindful.android.utils.AppConstants;
Expand All @@ -47,6 +49,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;

import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine;
Expand All @@ -58,9 +61,11 @@ public class MainActivity extends FlutterFragmentActivity implements MethodChann
private static final String TAG = "Mindful.MainActivity";
private SafeServiceConnection<MindfulTrackerService> mTrackerServiceConn;
private SafeServiceConnection<MindfulVpnService> mVpnServiceConn;
private SafeServiceConnection<MindfulNotificationListenerService> mNotificationServiceConn;
private SafeServiceConnection<FocusSessionService> mFocusServiceConn;
private ActivityResultLauncher<Intent> mVpnPermissionLauncher;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -84,12 +89,14 @@ protected void onCreate(Bundle savedInstanceState) {

// Initialize service connections
mTrackerServiceConn = new SafeServiceConnection<>(MindfulTrackerService.class, this);
mNotificationServiceConn = new SafeServiceConnection<>(MindfulNotificationListenerService.class, this);
mVpnServiceConn = new SafeServiceConnection<>(MindfulVpnService.class, this);
mFocusServiceConn = new SafeServiceConnection<>(FocusSessionService.class, this);

/// Bind to Services if they are already running
mTrackerServiceConn.bindService();
mVpnServiceConn.bindService();
mNotificationServiceConn.bindService();
mFocusServiceConn.bindService();
}

Expand All @@ -107,19 +114,18 @@ private void updateLocale(@NonNull String languageCode) {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);

MethodChannel mMethodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), AppConstants.FLUTTER_METHOD_CHANNEL);
mMethodChannel.setMethodCallHandler(this);
// Check if the was restarted itself during databased import
boolean isRestart = getIntent().getBooleanExtra(INTENT_EXTRA_IS_SELF_RESTART, false);
String appPackage = getIntent().getStringExtra(INTENT_EXTRA_PACKAGE_NAME);
if (isRestart) {
mMethodChannel.invokeMethod("updateIsRestartBool", true);
}
// Check if the app was launched from TLE dialog and update the targeted app
else if (appPackage != null && !appPackage.isEmpty()) {
mMethodChannel.invokeMethod("updateTargetedApp", appPackage);
}

// Get and set intent data
Intent currentIntent = getIntent();
Map<String, Object> intentData = new HashMap<>();
intentData.put("route", currentIntent.getStringExtra(INTENT_EXTRA_INITIAL_ROUTE));
intentData.put("targetedPackage", currentIntent.getStringExtra(INTENT_EXTRA_PACKAGE_NAME));
intentData.put("isSelfRestart", currentIntent.getBooleanExtra(INTENT_EXTRA_IS_SELF_RESTART, false));

// Update intent data on flutter side
mMethodChannel.invokeMethod("updateIntentData", intentData);
}

@Override
Expand All @@ -136,6 +142,11 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
result.success(true);
break;
}
case "setDataResetTime": {
SharedPrefsHelper.getSetDataResetTimeMins(this, call.arguments() == null ? 0 : call.arguments());
result.success(true);
break;
}
case "getDeviceInfoMap": {
result.success(Utils.getDeviceInfoMap(this));
break;
Expand All @@ -148,13 +159,12 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
);
break;
}
case "getShortsScreenTimeMs": {
result.success(SharedPrefsHelper.getSetShortsScreenTimeMs(this, null));
case "getUpComingNotifications": {
result.success(SharedPrefsHelper.getUpComingNotificationsArrayString(this));
break;
}
case "setDataResetTime": {
SharedPrefsHelper.getSetDataResetTimeMins(this, call.arguments() == null ? 0 : call.arguments());
result.success(true);
case "getShortsScreenTimeMs": {
result.success(SharedPrefsHelper.getSetShortsScreenTimeMs(this, null));
break;
}
case "getNativeCrashLogs": {
Expand Down Expand Up @@ -239,28 +249,40 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
result.success(true);
break;
}

// SECTION: Permissions handler methods ------------------------------------------------------
case "getAndAskNotificationPermission": {
result.success(NotificationHelper.getAndAskNotificationPermission(this, this, Boolean.TRUE.equals(call.arguments())));
case "updateDistractingNotificationApps": {
HashSet<String> distractingApps = SharedPrefsHelper.getSetNotificationBatchedApps(this, Utils.notNullStr(call.arguments()));
if (mNotificationServiceConn.isConnected()) {
mNotificationServiceConn.getService().updateDistractingApps(distractingApps);
} else if (!distractingApps.isEmpty()) {
mNotificationServiceConn.setOnConnectedCallback(service -> service.updateDistractingApps(distractingApps));
mNotificationServiceConn.bindService();
}
result.success(true);
break;
}
case "updateNotificationBatchSchedules": {
HashSet<Integer> todMinutes = SharedPrefsHelper.getSetNotificationBatchSchedules(this, Utils.notNullStr(call.arguments()));
if (!todMinutes.isEmpty()) {
AlarmTasksSchedulingHelper.scheduleNotificationBatchTask(this, todMinutes);
} else {
AlarmTasksSchedulingHelper.cancelNotificationBatchTask(this);
}
result.success(true);
break;
}
// SECTION: Permissions handler methods ------------------------------------------------------
case "getAndAskAccessibilityPermission": {
result.success(PermissionsHelper.getAndAskAccessibilityPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskDndPermission": {
result.success(PermissionsHelper.getAndAskDndPermission(this, Boolean.TRUE.equals(call.arguments())));
case "getAndAskAdminPermission": {
result.success(PermissionsHelper.getAndAskAdminPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskUsageAccessPermission": {
result.success(PermissionsHelper.getAndAskUsageAccessPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskAdminPermission": {
result.success(PermissionsHelper.getAndAskAdminPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskIgnoreBatteryOptimizationPermission": {
result.success(PermissionsHelper.getAndAskIgnoreBatteryOptimizationPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
Expand All @@ -273,6 +295,18 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
result.success(PermissionsHelper.getAndAskExactAlarmPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskNotificationPermission": {
result.success(PermissionsHelper.getAndAskNotificationPermission(this, this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskDndPermission": {
result.success(PermissionsHelper.getAndAskDndPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskNotificationAccessPermission": {
result.success(PermissionsHelper.getAndAskNotificationAccessPermission(this, Boolean.TRUE.equals(call.arguments())));
break;
}
case "getAndAskVpnPermission": {
result.success(getAndAskVpnPermission(Boolean.TRUE.equals(call.arguments())));
break;
Expand Down Expand Up @@ -368,6 +402,7 @@ protected void onDestroy() {

// Unbind services
mTrackerServiceConn.unBindService();
mNotificationServiceConn.unBindService();
mVpnServiceConn.unBindService();
mFocusServiceConn.unBindService();
}
Expand Down
Loading

0 comments on commit d018e86

Please sign in to comment.