0

Android provides the developer to declare the permission before an app can uses tools or hardware, I have created a class to store each permission's description like the permission name, nice name , description like what that permission does. Now is there anyway i can initialize each object programmatically, getting the information from http://developer.android.com/reference/android/Manifest.permission.html.

The code for the class is

package org.owasp.seraphimdroid.customclasses;

public class PermissionData {
    private String permission;
    private String permissionName;
    private String description;
    private String regularUseDescription;
    private String maliciousUseDescription;
    private int weight;

    public PermissionData(String permission){
        this.permission = permission;
        setData();
    }

    private void setData(){
        weight = 0;
    }   

    //Getters and setter
    public String getPermissionName() {
        return permissionName;
    }
    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRegularUseDescription() {
        return regularUseDescription;
    }

    public void setRegularUseDescription(String regularUseDescription) {
        this.regularUseDescription = regularUseDescription;
    }

    public String getMaliciousUseDescription() {
        return maliciousUseDescription;
    }

    public void setMaliciousUseDescription(String maliciousUseDescription) {
        this.maliciousUseDescription = maliciousUseDescription;
    }



}

Also should i store these objects as hashmap or in database? These will mostly be used to display information in activity according to the permission.

Assassin
  • 1
  • 2

1 Answers1

0

Use the Context.check* methods (methods from the Context object that start with "check") for checking if a given permission is granted. See an example here.

Please note that permissions cannot be added at runtime.

The simplest ways to store your objects' data that I can think of at the moment are writing them to a database, serializing them into a file, or writing key-value pairs to SharedPreferences. It will depend on what you think is more appropriate. A hashmap has nothing to do with persistence; you may choose it as the approach to keep your data in memory and access it during the app's execution.

Training for Android developers has a section on writing key-value pairs and database persistence. If you wish to use serialization, the methods below might be useful:

private void _serializeObject(Object object, String fileName) {
    String aboluteFilePath = getApplicationContext().getFilesDir().getPath() + File.separator + fileName;
    try {

        FileOutputStream fileOut = new FileOutputStream(absoluteFilePath);
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(object);
        out.close();
        fileOut.close();
    } catch (IOException e) {
        Log.e(TAG, "Error while serializing data to " + absoluteFilePath, e);
    }
}

private Object _deserializeObject(String fileName) {
    Object object = null;
    String absoluteFilePath = getApplicationContext().getFilesDir().getPath() + File.separator + fileName;
    try {
        FileInputStream fileIn = new FileInputStream(absoluteFilePath);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        object = in.readObject();
        in.close();
        fileIn.close();
    } catch (FileNotFoundException e) {
        // You may want to ignore this exception as it will occur the first time the
        // data is deserialized unless you make sure serialization occurs before it.
    } catch (IOException e) {
        Log.e(TAG, IOException.class.getSimpleName() + " while deserializing from " + absoluteFilePath, e);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, ClassNotFoundException.class.getSimpleName() + " while deserializing from " + absoluteFilePath, e);
    }

    return object;
}
Community
  • 1
  • 1
Piovezan
  • 3,215
  • 1
  • 28
  • 45