<commit_before>#include <notify.h>
#import "MBWiFiProxyInfo.h"
#import "LSStatusBarItem.h"
#import "MBProxyProfilesDisplayer.h"

static BOOL enabled = NO;
static BOOL alwaysShow = YES;
static NSUInteger type = 0;
static MBWiFiProxyInfo *proxyInfo;
static LSStatusBarItem *statusBarItem;

@interface SBWiFiManager : NSObject
+ (instancetype)sharedInstance;
- (void)_powerStateDidChange;
- (NSString *)currentNetworkName;
- (BOOL)wiFiEnabled;
@end

@interface RadiosPreferences : NSObject
@property (nonatomic) BOOL airplaneMode;
@end

@interface UIStatusBarItem : NSObject 
@property (nonatomic, readonly) NSString *indicatorName;
@end

@interface UIStatusBarItemView : UIView
@property (nonatomic, strong, readonly) UIStatusBarItem *item;
@end

static BOOL canShowStatusBarIcon() {
    return alwaysShow ? YES : [[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName];
}

static void updateStatusBarImage() {
    statusBarItem.imageName = type > 0 ? @"ProxySwitcher" : @"ProxySwitcherUnselected";
}

static void setStatusBarVisible(BOOL visible) {
    statusBarItem.visible = enabled && canShowStatusBarIcon() ? visible : NO; 
}

static void networkChanged() {
    if (!enabled) { return; }
    setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
    if ([[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName]) {
        if (type == 1) {
            notify_post("com.mikaelbo.proxyswitcherd.enable"); 
        } else {
            notify_post("com.mikaelbo.proxyswitcherd.disable");
        }
    }
}

static void loadPreferences() {
    CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mikaelbo.proxyswitcher"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    NSDictionary *preferences;
    if (keyList) {
        preferences = (__bridge NSDictionary *)CFPreferencesCopyMultiple(keyList, 
                                                                   CFSTR("com.mikaelbo.proxyswitcher"), 
                                                                   kCFPreferencesCurrentUser, 
                                                                   kCFPreferencesAnyHost);
        if (!preferences) { preferences = [NSDictionary dictionary]; }
        CFRelease(keyList);
        enabled = [preferences objectForKey:@"enabled"] ? [[preferences objectForKey:@"enabled"] boolValue] : NO;
        alwaysShow = [preferences objectForKey:@"alwaysShow"] ? [[preferences objectForKey:@"alwaysShow"] boolValue] : YES;
        proxyInfo = [MBWiFiProxyInfo infoFromDictionary:preferences];
        type = [preferences objectForKey:@"type"] ? [[preferences objectForKey:@"type"] integerValue] : 0;
        notify_post("com.mikaelbo.proxyswitcherd.refreshPreferences");
        setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
        updateStatusBarImage();
        networkChanged();
    }
}

static void saveNewType() {
    CFPreferencesSetAppValue(CFSTR("type"),
                            CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &type),
                            CFSTR("com.mikaelbo.proxyswitcher"));
    networkChanged();
    updateStatusBarImage();
}

static void hideProfilesOverlayIfNeeded() {
    [[MBProxyProfilesDisplayer sharedDisplayer] hideProxyProfilesIfNeeded];
}


%hook SBHomeHardwareButton

- (void)doubleTapUp:(id)arg1 {
    hideProfilesOverlayIfNeeded();
    %orig;
}
<commit_msg>Fixed home button handling for older devices<commit_after>#include <notify.h>
#import "MBWiFiProxyInfo.h"
#import "LSStatusBarItem.h"
#import "MBProxyProfilesDisplayer.h"

static BOOL enabled = NO;
static BOOL alwaysShow = YES;
static NSUInteger type = 0;
static MBWiFiProxyInfo *proxyInfo;
static LSStatusBarItem *statusBarItem;

@interface SBWiFiManager : NSObject
+ (instancetype)sharedInstance;
- (void)_powerStateDidChange;
- (NSString *)currentNetworkName;
- (BOOL)wiFiEnabled;
@end

@interface RadiosPreferences : NSObject
@property (nonatomic) BOOL airplaneMode;
@end

@interface UIStatusBarItem : NSObject 
@property (nonatomic, readonly) NSString *indicatorName;
@end

@interface UIStatusBarItemView : UIView
@property (nonatomic, strong, readonly) UIStatusBarItem *item;
@end

static BOOL canShowStatusBarIcon() {
    return alwaysShow ? YES : [[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName];
}

static void updateStatusBarImage() {
    statusBarItem.imageName = type > 0 ? @"ProxySwitcher" : @"ProxySwitcherUnselected";
}

static void setStatusBarVisible(BOOL visible) {
    statusBarItem.visible = enabled && canShowStatusBarIcon() ? visible : NO; 
}

static void networkChanged() {
    if (!enabled) { return; }
    setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
    if ([[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName]) {
        if (type == 1) {
            notify_post("com.mikaelbo.proxyswitcherd.enable"); 
        } else {
            notify_post("com.mikaelbo.proxyswitcherd.disable");
        }
    }
}

static void loadPreferences() {
    CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mikaelbo.proxyswitcher"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    NSDictionary *preferences;
    if (keyList) {
        preferences = (__bridge NSDictionary *)CFPreferencesCopyMultiple(keyList, 
                                                                   CFSTR("com.mikaelbo.proxyswitcher"), 
                                                                   kCFPreferencesCurrentUser, 
                                                                   kCFPreferencesAnyHost);
        if (!preferences) { preferences = [NSDictionary dictionary]; }
        CFRelease(keyList);
        enabled = [preferences objectForKey:@"enabled"] ? [[preferences objectForKey:@"enabled"] boolValue] : NO;
        alwaysShow = [preferences objectForKey:@"alwaysShow"] ? [[preferences objectForKey:@"alwaysShow"] boolValue] : YES;
        proxyInfo = [MBWiFiProxyInfo infoFromDictionary:preferences];
        type = [preferences objectForKey:@"type"] ? [[preferences objectForKey:@"type"] integerValue] : 0;
        notify_post("com.mikaelbo.proxyswitcherd.refreshPreferences");
        setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
        updateStatusBarImage();
        networkChanged();
    }
}

static void saveNewType() {
    CFPreferencesSetAppValue(CFSTR("type"),
                            CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &type),
                            CFSTR("com.mikaelbo.proxyswitcher"));
    networkChanged();
    updateStatusBarImage();
}

static void hideProfilesOverlayIfNeeded() {
    [[MBProxyProfilesDisplayer sharedDisplayer] hideProxyProfilesIfNeeded];
}


%hook SBUIController

- (BOOL)handleMenuDoubleTap {
    hideProfilesOverlayIfNeeded();
    return %orig;
}

- (BOOL)isHandlingHomeButtonPress {
    hideProfilesOverlayIfNeeded();
    return %orig;
}

- (_Bool)clickedMenuButton {
    hideProfilesOverlayIfNeeded();
    return %orig;
}

%end


%hook SBHomeHardwareButton

- (void)doubleTapUp:(id)arg1 {
    hideProfilesOverlayIfNeeded();
    %orig;
}

{'fraction_non_alphanumeric': 0.1093899931459904, 'fraction_numerical': 0.0019191226867717615, 'mean_word_length': 3.5829145728643215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4340088', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2134, 'n_words': 403}