Last active
April 30, 2018 21:07
-
-
Save patrickhammond/b13defa62a444cd22936 to your computer and use it in GitHub Desktop.
Permissions + SumType (https://github.com/madebyatomicrobot/sumtype)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// annotation processor will generate DevicePhoneNumberSumType (see: 4_DevicePhoneNumberSumType.java) | |
@SumType | |
public interface DevicePhoneNumber { | |
String phoneNumber(); | |
void phoneNumberNotAvailable(); | |
String[] permissionsRequired(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lives on the application, not activity | |
@Produce | |
public DevicePhoneNumberSumType produceCurrentPhoneNumber() { | |
return buildDevicePhoneNumberSumType(); | |
} | |
@Subscribe | |
public void handleGetPhoneNumber(GetDevicePhoneNumber event) { | |
bus.post(buildDevicePhoneNumber()); | |
} | |
private DevicePhoneNumberSumType buildDevicePhoneNumber() { | |
String[] permissionsRequired = { android.Manifest.permission.READ_PHONE_STATE }; | |
if (PermissionUtil.checkPermissionsGranted(application, permissionsRequired)) { | |
TelephonyManager phoneManager = (TelephonyManager) application.getSystemService(Context.TELEPHONY_SERVICE); | |
String phoneNumber = phoneManager.getLine1Number(); | |
if (!TextUtils.isEmpty(phoneNumber)) { | |
return DevicePhoneNumberSumType.ofPhoneNumber(phoneNumber); | |
} else { | |
return DevicePhoneNumberSumType.ofPhoneNumberNotAvailable(); | |
} | |
} else { | |
return DevicePhoneNumberSumType.ofPermissionsRequired(permissionsRequired); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// subscriber lives on the activity | |
@Subscribe | |
public void handleDevicePhoneNumber(DevicePhoneNumberSumType devicePhoneNumber) { | |
devicePhoneNumber.accept(new DevicePhoneNumberSumTypeVisitor() { | |
@Override | |
public void visitPhoneNumber(String phoneNumber) { | |
// Display the phone number | |
} | |
@Override | |
public void visitPhoneNumberNotAvailable() { | |
} | |
@Override | |
public void visitPermissionsRequired(String[] permissionsRequired) { | |
// Request permissions | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HERE FOR REFERENCE ONLY - GENERATED AT COMPILE TIME BY ANNOTATION PROCESSOR | |
import java.lang.IllegalStateException; | |
import java.lang.Object; | |
import java.lang.Override; | |
import java.lang.String; | |
/** | |
* Generated sum type class for {@link DevicePhoneNumber} */ | |
public final class DevicePhoneNumberSumType implements DevicePhoneNumber { | |
private static final Object voidPlaceholder = new Object(); | |
private final String phoneNumber; | |
private final Object phoneNumberNotAvailable; | |
private final String[] permissionsRequired; | |
private DevicePhoneNumberSumType(String phoneNumber, Object phoneNumberNotAvailable, String[] permissionsRequired) { | |
this.phoneNumber = phoneNumber; | |
this.phoneNumberNotAvailable = phoneNumberNotAvailable; | |
this.permissionsRequired = permissionsRequired; | |
} | |
public static DevicePhoneNumberSumType ofPhoneNumber(String phoneNumber) { | |
if (phoneNumber == null) { | |
throw new IllegalArgumentException(); | |
} | |
return new DevicePhoneNumberSumType(phoneNumber, null, null); | |
} | |
@Override | |
public String phoneNumber() { | |
if (phoneNumber == null) { | |
throw new IllegalStateException(); | |
} | |
return phoneNumber; | |
} | |
public static DevicePhoneNumberSumType ofPhoneNumberNotAvailable() { | |
return new DevicePhoneNumberSumType(null, voidPlaceholder, null); | |
} | |
@Override | |
public void phoneNumberNotAvailable() { | |
if (phoneNumberNotAvailable == null) { | |
throw new IllegalStateException(); | |
} | |
} | |
public static DevicePhoneNumberSumType ofPermissionsRequired(String[] permissionsRequired) { | |
if (permissionsRequired == null) { | |
throw new IllegalArgumentException(); | |
} | |
return new DevicePhoneNumberSumType(null, null, permissionsRequired); | |
} | |
@Override | |
public String[] permissionsRequired() { | |
if (permissionsRequired == null) { | |
throw new IllegalStateException(); | |
} | |
return permissionsRequired; | |
} | |
public void accept(DevicePhoneNumberSumTypeVisitor visitor) { | |
if (phoneNumber != null) { | |
visitor.visitPhoneNumber(phoneNumber); | |
return; | |
} | |
if (phoneNumberNotAvailable != null) { | |
visitor.visitPhoneNumberNotAvailable(); | |
return; | |
} | |
if (permissionsRequired != null) { | |
visitor.visitPermissionsRequired(permissionsRequired); | |
return; | |
} | |
} | |
public interface DevicePhoneNumberSumTypeVisitor { | |
void visitPhoneNumber(String phoneNumber); | |
void visitPhoneNumberNotAvailable(); | |
void visitPermissionsRequired(String[] permissionsRequired); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.v4.content.ContextCompat; | |
import static android.content.pm.PackageManager.PERMISSION_GRANTED; | |
public class PermissionUtil { | |
public static boolean checkPermissionsGranted(Context context, String...permissions) { | |
for (String permission : permissions) { | |
if (ContextCompat.checkSelfPermission(context, permission) != PERMISSION_GRANTED) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment