Question

I'm building a MobileSubstrate tweak using Logos, and I'm attempting to add a new method/s to lock the device into every application on the device, which would be run after a proximity change notification. So far, my code is

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <SpringBoard/SpringBoard.h>
#import <SpringBoard/UIApplicationDelegate.h>
#import <GraphicsServices/GSEvent.h>
#include <notify.h>

@interface suspendresume : NSObject 

@property(nonatomic, readonly) BOOL proximityState;

@end

@implementation suspendresume

BOOL tweakOn;

@end

static NSString *settingsFile = @"/var/mobile/Library/Preferences/com.matchstick.suspendresume.plist";

%hook SpringBoard

-(void)applicationDidFinishLaunching:(id)application {
    // Allow SpringBoard to initialise
    %orig;

    // Set up proximity monitoring
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    [[UIDevice currentDevice] proximityState];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
}

%new

// Add new code into SpringBoard
-(void)proximityChange:(NSNotification*)notification {
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];

    // Check if tweak is on
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:settingsFile];
    tweakOn = [[dict objectForKey:@"enabled"] boolValue];

    // Only run if tweak is on
    if (tweakOn) {

        // Get first proximity value
        if ([[UIDevice currentDevice] proximityState] == YES) {

            // Wait a few seconds TODO allow changing of wait interval from prefrences FIXME causes a lockup of interface whilst sleeping
            [self performSelector:@selector(lockDeviceAfterDelay) withObject:nil afterDelay:1.0];
        }
    }
}

%new

-(void)lockDeviceAfterDelay {

    // Second proximity value
    if ([[UIDevice currentDevice] proximityState] == YES) {

        // Lock device
        GSEventLockDevice();
    }
}

%end

which works as I require in SpringBoard, but not in any other applications installed on the device - all that happens when testing is that the display is turned off when the proximity sensor is triggered, and doesn't lock the device.

I'm thinking of utilising UIApplicationDelegate's -(void)applicationDidFinishLaunching:(id)application and UIApplication to achieve the same in applications as I have with SpringBoard, but can't figure out how to do this.

The idea for this approach came from this project

Do I need to add the same code I have running in SpringBoard into a new method/s under UIApplication?

Do I need to re-setup proximity monitoring for each application, and how would I call these new methods to run after a proximity change notification is received?

Also, the full source for this is on my GitHub

Was it helpful?

Solution

Turns out this wasn't the right way to do this. Instead,

// Get the topmost application
SBApplication *runningApp = [(SpringBoard *)self _accessibilityFrontMostApplication];
// We're in application, resign app
[runningApp notifyResignActiveForReason:1];

did the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top