Wednesday, 22 October 2014

Unlocking Android lock screen HATE

Today I wanted to loan my CyanogenMod 9 phone to a friend and temporarily remove the PIN from the lock screen. But the options "None" and "Slide" were greyed out in the lock screen's security settings where a message said "Disabled by administrator, encryption policy or credential storage".

Needless to say, I am my own phone's administrator and I have done no such thing and there is no encryption policy or credential storage installed. So the message that is trying to be helpful is completely useless and the effect is that I cannot disable security on my *own* phone! Argh.

Some googling turned up a hint that it could be related to a VPN connection. In fact, I have a VPN connection to my home network stored on the phone, and after deleting it I was once again able to select all lock screen options.

The amount of time this took me in addition to the time it will take for trial and error to get the VPN working again is just ridiculous. A simple task like temporarily disabling lock screen security shouldn't involve guessing which random setting anywhere in the phone is preventing an option from being selected. At the very least, there should be a clear indication what the problem is, preferably with a button to override it next to it.

Some mumbo-jumbo about "policies" or "credentials" just doesn't cut it. The sort of user interface as it currently stands is unacceptable.

Saturday, 18 October 2014

make_shared (and make_unique) for classes with private constructors

A common idiom is to have a privately constructable class with a (static) factory that returns a shared (or unique) pointer.

class A {
    A() {};

public:
    static std::shared_ptr<A> make() {
        return std::make_shared<A>();
    }
};
Looks simple and yet doesn't work since std::make_shared cannot access A's constructor.

I found a simple trick how to solve this without opening the class up to public construction: add a parameter with a private »cookie« to the constructor which proves that the class is being constructed »from within«.

class A {
    struct ctor_cookie {};

public:
    explicit A(ctor_cookie) {};
    static std::shared_ptr<A> make() {
        return std::make_shared<A>(ctor_cookie());
    }
};