2

我正在使用 Logos 构建一个 MobileSubstrate 调整,并且我正在尝试添加一种新方法来将设备锁定到设备上的每个应用程序中,这将在接近更改通知之后运行。到目前为止,我的代码是

#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

它可以按我在 SpringBoard 中的要求工作,但不能在设备上安装的任何其他应用程序中工作 - 测试时发生的所有事情都是当接近传感器被触发时显示器关闭,并且不会锁定设备。

我正在考虑使用 UIApplicationDelegate -(void)applicationDidFinishLaunching:(id)applicationUIApplication在应用程序中实现与 SpringBoard 相同的功能,但不知道如何做到这一点。

这种方法的想法来自这个项目

我是否需要将在 SpringBoard 中运行的相同代码添加到新方法中UIApplication

我是否需要为每个应用程序重新设置接近度监控,以及在收到接近度更改通知后如何调用这些新方法运行?

另外,我的 GitHub 上有完整的源代码

4

1 回答 1

2

事实证明这不是正确的方法。反而,

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

成功了。

于 2013-03-29T20:56:22.460 回答