2

I have a small test app, that attempts to display the call log.

Physical phones that I have tested the test app are : LG G2 Android 4.4.2 Kernel 3.4.0 And Samsung S5 Android 4.4.2 Kernel 3.4.0.

I have given the app these permissions:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="acl.test.com.acl" >

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CALL_LOG" />
    <uses-permission android:name="android.permission.Calls.CONTENT_URI" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="acl.test.com.acl.aclApp">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public static String appTagForLog = "ACL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        boolean hasREAD_CALL_LOG = SecurityUtil.doesUserHavePermission(SecurityUtil.READ_CALL_LOG);
        boolean hasCONTENT_URI = SecurityUtil.doesUserHavePermission(SecurityUtil.CONTENT_URI);
        boolean hasREAD_CONTACTS = SecurityUtil.doesUserHavePermission(SecurityUtil.READ_CONTACTS);
        try {


            //check security
            if (hasREAD_CALL_LOG && hasCONTENT_URI && hasREAD_CONTACTS) {
                Log.d(appTagForLog, "app has all permissions");
                StringBuffer sb = CallLogService.getCallLog();
                Log.d(appTagForLog, sb.toString());

            } else {
                //kind of error, app will not be useful alert user
                String messageToUser = "The user must enable ";
                if (hasREAD_CALL_LOG) {
                    messageToUser = messageToUser + "READ CALL LOG";
                }
                if (hasCONTENT_URI) {
                    messageToUser = messageToUser + ", CALL DATA";
                }
                if (hasREAD_CONTACTS) {
                    messageToUser = messageToUser + ", READ_CONTACTS";
                }

                messageToUser = messageToUser + " permissions!";

                Toast.makeText(
                        this,
                        messageToUser,
                        Toast.LENGTH_LONG).show();

                Log.d(appTagForLog, messageToUser);

                StringBuffer sb = CallLogService.getCallLog();
                Log.d(appTagForLog, "***CALL LOG:"+ sb.toString());

            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
            Log.d(appTagForLog, "Exception, message: "+exc.getMessage());
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}//end of main activity 

I have added logic in the MainActivity that checks whether READ_CALL_LOG & READ CONTACTS was granted, however I keep getting false.

Why are declared permissions not being granted & how can they be forced to be granted???

Thanks

1
  • 1
    Perhaps there is a bug in your implementation of SecurityUtil.doesUserHavePermission(). Also note that there is no android.permission.Calls.CONTENT_URI permission in Android. Commented Nov 29, 2015 at 1:27

2 Answers 2

1

According to the docs:

To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission.

0

If permission is declared in manifest, this should work for you.

  public static boolean checkPermission(String permission, Context mContext) {
    return mContext.getPackageManager().checkPermission(permission,
        mContext.getPackageName()) == PackageManager.PERMISSION_GRANTED;
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.