// Path: app/src/main/java/com/livingstoneapp/models/DeclaredPermission.java
// public class DeclaredPermission {
// 
//     private String mName;
//     private String mLabel;
//     private ArrayList<String> mProtectionLevel;
//     private ArrayList<String> mFlags;
// 
//     public DeclaredPermission(String mName, String mLabel, ArrayList<String> mProtectionLevel, ArrayList<String> mFlags) {
//         this.mLabel = mLabel;
//         this.mName = mName;
//         this.mProtectionLevel = mProtectionLevel;
//         this.mFlags = mFlags;
//     }
// 
//     public String getLabel() {
//         return mLabel;
//     }
// 
//     public String getName() {
//         return mName;
//     }
// 
//     public ArrayList<String> getProtectionLevel() {
//         return mProtectionLevel;
//     }
// 
//     public ArrayList<String> getFlags() { return mFlags; }
// }
// 
// Path: app/src/main/java/com/livingstoneapp/models/RequestedPermission.java
// public class RequestedPermission {
//     private String mName;
//     private boolean mGranted;
// 
//     public RequestedPermission(String mName, boolean mGranted) {
//         this.mGranted = mGranted;
//         this.mName = mName;
//     }
// 
//     public boolean isGranted() {
//         return mGranted;
//     }
// 
//     public String getName() {
//         return mName;
//     }
// }
// 
// Path: app/src/main/java/com/livingstoneapp/utils/Utils.java
// public class Utils {
//     public static boolean hasFlag(
//             int flags,
//             int flagToCheck
//     ) {
//         return ((flags & flagToCheck) == flagToCheck);
//     }
// }


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.os.Build;
import java.util.ArrayList;
import rx.Observable;
import com.livingstoneapp.models.DeclaredPermission;
import com.livingstoneapp.models.RequestedPermission;
import com.livingstoneapp.utils.Utils;
package com.livingstoneapp.interactors;




/**
 * Created by renier on 2/19/2016.
 */
public class PermissionsInteractorImpl implements IPermissionsInteractor {
    private Context mContext;

    public PermissionsInteractorImpl(Context context) {
        mContext = context;
    }

    @Override
    public Observable<ArrayList<DeclaredPermission>> getDeclaredPermissions(String packageName) {
        return Observable.defer(() -> Observable.create(subscriber -> {
            try {
                ArrayList<DeclaredPermission> toReturn = new ArrayList<>();

                PackageManager packageManager = mContext.getPackageManager();
                PackageInfo pi = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);

                if (pi.permissions != null) {
                    for (PermissionInfo info : pi.permissions) {
                        ArrayList<String> protectionLevels = new ArrayList<>();
                        ArrayList<String> flags = new ArrayList<>();

                        //Protection Levels
// Path: app/src/main/java/com/livingstoneapp/models/DeclaredPermission.java
// public class DeclaredPermission {
// 
//     private String mName;
//     private String mLabel;
//     private ArrayList<String> mProtectionLevel;
//     private ArrayList<String> mFlags;
// 
//     public DeclaredPermission(String mName, String mLabel, ArrayList<String> mProtectionLevel, ArrayList<String> mFlags) {
//         this.mLabel = mLabel;
//         this.mName = mName;
//         this.mProtectionLevel = mProtectionLevel;
//         this.mFlags = mFlags;
//     }
// 
//     public String getLabel() {
//         return mLabel;
//     }
// 
//     public String getName() {
//         return mName;
//     }
// 
//     public ArrayList<String> getProtectionLevel() {
//         return mProtectionLevel;
//     }
// 
//     public ArrayList<String> getFlags() { return mFlags; }
// }
// 
// Path: app/src/main/java/com/livingstoneapp/models/RequestedPermission.java
// public class RequestedPermission {
//     private String mName;
//     private boolean mGranted;
// 
//     public RequestedPermission(String mName, boolean mGranted) {
//         this.mGranted = mGranted;
//         this.mName = mName;
//     }
// 
//     public boolean isGranted() {
//         return mGranted;
//     }
// 
//     public String getName() {
//         return mName;
//     }
// }
// 
// Path: app/src/main/java/com/livingstoneapp/utils/Utils.java
// public class Utils {
//     public static boolean hasFlag(
//             int flags,
//             int flagToCheck
//     ) {
//         return ((flags & flagToCheck) == flagToCheck);
//     }
// }


// Path: app/src/main/java/com/livingstoneapp/interactors/PermissionsInteractorImpl.java
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.os.Build;
import java.util.ArrayList;
import rx.Observable;
import com.livingstoneapp.models.DeclaredPermission;
import com.livingstoneapp.models.RequestedPermission;
import com.livingstoneapp.utils.Utils;

package com.livingstoneapp.interactors;




/**
 * Created by renier on 2/19/2016.
 */
public class PermissionsInteractorImpl implements IPermissionsInteractor {
    private Context mContext;

    public PermissionsInteractorImpl(Context context) {
        mContext = context;
    }

    @Override
    public Observable<ArrayList<DeclaredPermission>> getDeclaredPermissions(String packageName) {
        return Observable.defer(() -> Observable.create(subscriber -> {
            try {
                ArrayList<DeclaredPermission> toReturn = new ArrayList<>();

                PackageManager packageManager = mContext.getPackageManager();
                PackageInfo pi = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);

                if (pi.permissions != null) {
                    for (PermissionInfo info : pi.permissions) {
                        ArrayList<String> protectionLevels = new ArrayList<>();
                        ArrayList<String> flags = new ArrayList<>();

                        //Protection Levels
                        if (Utils.hasFlag(info.protectionLevel, PermissionInfo.PROTECTION_DANGEROUS))