Unit test for uses-permission permissions - android

I'm trying to figure a way to test if <uses-permission android:name="android.permission.INTERNET" /> is set in AndroidManifest.xml, but I can't. Is it possible to do that? Right now I'm using ActivityInstrumentationTestCase2.
I'm trying to learn testing in Android Studio, and I think that testing that certain uses-permission permissions are set is a good idea at the start of testing the application.

This is how I ended up doing it:
Context testContext = getActivity().getContext();
PackageManager pm = testContext.getPackageManager();
expected = PackageManager.PERMISSION_GRANTED;
actual = pm.checkPermission(Manifest.permission.<PERMISSION_YOU_ARE_LOOKING_FOR>, testContext.getPackageName());
assertEquals(expected, actual);

Related

Appcelerator SDK 5.4 Android Calendar permission on Android 6

I've read a really lot of posts yet, but the problem still occurs...
My app needs calendar permission, and this wil work fine till Android 5.
In my trap.xml in the permissions ar set as followed:
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
</manifest>
</android>
When I try to set the permission like I found on Jira:
function calenderTask() {
if (Ti.Calendar.hasCalendarPermissions()) {
showCalendars(Ti.Calendar.selectableCalendars);
} else {
Ti.Calendar.requestCalendarPermissions(function(e) {
if (e.success) {
showCalendars(Ti.Calendar.selectableCalendars);
} else {
Ti.API.error(e.error);
alert('Access to calendar is not allowed');
}
});
}
}
function showCalendars(calendars) {
for (var i = 0; i < calendars.length; i++) {
Ti.API.info("Calender: "+calendars[i].name);
}
}
calenderTask();
only after a fresh install on the device (Samgung S7, Android 6) there is some action when I place an alert in the code.
The next run it looks like the code is ignored.
And... most important of all, there is no permissions set.
When I manually set the permission in the settings of my device, the app works fine.
Please is there somebody with a solution for me?
I'm also trying to find a better way of checking Calendar permissions on Android 6.
Meanwhile, I'm using this code to send the user right to the app settings on a dialog:
var dialog = Ti.UI.createAlertDialog();
dialog.message = 'Please check your calendar permissions';
dialog.cancel = 1;
dialog.buttonNames = ['Settings', 'Cancel'];
dialog.addEventListener('click', function(e){
var intentData = {action: "android.settings.APPLICATION_DETAILS_SETTINGS", data: "package:" + Ti.App.getId()};
var flags = [ Ti.Android.FLAG_ACTIVITY_NEW_TASK, Ti.Android.FLAG_ACTIVITY_NO_HISTORY, Ti.Android.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS];
if (e.index === 0){
win.openIntent(intentData, flags);
}
});
dialog.show();
I hope it helps a bit...
I found a solution/workaround for my problem that the calendar permission was not asked for.
In the settings section of the device I removed the permissions by hand by the specific app. Now I did a reset on all settings of the installed app's and then the question is asked on the device when I start the particular part of the app.
Thus just remove the permissions and delete the app from the device is not enough!
I've used the part of code that I listed before, that from Jira.
So, for the moment my problem is solved (I think) and the app is asking for the needed permissions after first install.
Carlos, thanks for your comment, and I hope you can do something with my investigations too.

Android M - GoogleAccountCredential setSelectedAccount doesn't work - name must not be null

On the Android M-Preview GoogleAccountCredential.setSelectedAccount doesn't seem to work.
When debugging, I noticed that after calling that method, the selectedAccount and accountName fields of the object are still null.
While debugging you can see that my variable accountName is not empty or null, I call the .setSelectedAccountName(), but as you can see in the debug window, the field in the GoogleAccountCredential is still null.
I think this can be related to some permissions? On my Manifest, I have the following permissions declared:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
I know that the GET_ACCOUNTS permission you get for free on M (According to https://developer.android.com/preview/features/runtime-permissions.html#normal ), but the other two permissions are "unknown" to the M-Preview. So maybe, it's that?
android.permission.GET_ACCOUNTS has protectionLevel:dangerous and is now part of the Contacts permission group, which means you should request it at runtime using the new Activity.requestPermissions()
Only then can you interact with accounts created by other apps on your device.
I had the same issue.
Use setSelectedAccount() instead of setSelectedAccountName()
mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff());
// to set accountName manually instead of prompting user to select it
mCredential.setSelectedAccount(new Account("xyz#gmail.com", "com.android.example"));
set the account that you want to set as the email and you can give your package name as the second parameter.

Toggle GPS Programmatically Android 4.4

I know. Don't do this. I don't care. It's for a root app.
The app is installed to /system/app/ with 0777 permission
I was previously using:
ContentResolver cr = context.getContentResolver();
Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, !isGpsOn);
This is how I'm trying it on 4.4 since that was deprecated:
int value;
if (isGpsOn)value = Settings.Secure.LOCATION_MODE_OFF;
else value = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
Settings.Secure.putInt(cr, Settings.Secure.LOCATION_MODE, value);
And it is silently failing (according to some user reports). How can I properly toggle GPS with Android 4.4 from an app in System folder?
Don't do this. :)
Check the code for GPS TOGGLER
GPS toggle widget for Android rooted devices. It wrks well even for
those ROMs and kernels, other software failed.
With your code, just checking:
Hope you declared the following permissions in your manifest:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
I had used this on rooted LG Optimus, android 4.0/4.1.
Reportedly this may no longer work but i am not sure about it for all devices and o.s.:
Switch it ON/OFF with a flag check and this code:
try {
Settings.Secure.putString (context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED, String.format ("%s,%s",
Settings.Secure.getString (context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED), LocationManager.GPS_PROVIDER));
} catch(Exception e) {}

Activity Access Restriction Implementation in a Monodroid Application

I have a requirement where I need to restrict access to an activity of a Monodroid Application. Hence i tried a spike where the application IntentSayHello would have an access restricted Activity called SayHelloActivity. As the first step i defined the permission tag in the AndroidManifest.xml of the application as below:
...
...
</application>
<permission
android:name="intentsayhello.permission.SAYHELLO"
android:protectionLevel="signature" android:label="#string/permlbl_restricted"
android:description="#string/permdesc_restricted">
</permission>
</manifest>
Please note that i'm using protectionLevel = signature which means that any other application signed with the same certificate as IntentSayHello can only access the restricted activity.
Now i coded the SayHelloActivity as below:
[Activity(Label = "SayHelloActivity", MainLauncher = true, Icon = "#drawable/icon", Permission = "intentsayhello.permission.SAYHELLO")]
[IntentFilter(new string[] { "companyXYZ.intent.sayhello.MAIN" },Categories = new string[]{Intent.CategoryDefault},
DataMimeType = "vnd.companyXYZ.say.hello/vnd.companyXYZ.activity")]
public class SayHelloActivity : Activity
{
.....
.....
}
After this i tested with a client application by invoking SayHelloActivity of IntentSayHello through an implicit intent and i got SecurityException as expected.
Permission Denial: starting Intent { act=companyXYZ.intent.sayhello.MAIN typ=vnd.companyXYZ.say.hello/vnd.companyXYZ.activity cmp=IntentSayHello.IntentSayHello/intentsayhello.SayHelloActivity }
from ProcessRecord{4094f850 9126:DiffKeyHello.DiffKeyHello/10097} (pid=9126, uid=10097) requires intentsayhello.permission.SAYHELLO
Now if i want my client Application to be given access to the SayHelloActivity of the restricted application, i'm supposed sign my client application with the same keystore (certificate) and also mention in the AndroidManifest.xml of the client application as below:
...
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="intentsayhello.permission.SAYHELLO" />
</manifest>
But when i did both of this, the client application still could not invoke the SayHelloActivity and same SecurityException is thrown.
I would like to know the directions/solution to this issue.
Thanks
After Googling around, hit upon this page: http://lists.ximian.com/pipermail/mono-bugs/2011-January/108218.html
I took the cue from this page that the need to be put into the Client's AssemblyInfo.cs. The syntax would be:
// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission("intentsayhello.permission.SAYHELLO")]
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
It worked from then on.
Further search in mono android forum, i found that another way of doing this is to explicitly add the androidmanifest.xml in the .csproj file as below:
<PropertyGroup>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
Either of the above solutions works, only i do not know which/both are correct practice.

Android Visualizer class throwing runtime exception

I am using AndroidFX Visualizer class in my demo app to read FFT but when i try to create object of that class its throwing Runtime exception (java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -1). Player class is my custom class for playback control and using same Player class i have implemented equalizer class and that's working fine. Do i need to add any permission in manifest file?
Player mediaPlayer = Player.GetInstance();
mediaPlayer.LoadFile("song.mp3");
mediaPlayer.Play();
try{
visual = new Visualizer(mediaPlayer.GetAudioSessionID()); // this line causing Exception
visual.setEnabled(true);
visual.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
}
catch(Exception ex)
{
Log.e("Visual Ex", ex.getMessage());
}
That was due to my foolish mistake, that feature requires <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> permission. thanks
I know this is a very late answer but I also struggled with this problem and I want to share my experiences.
First, as the answer above mentioned, the permissions
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
and, if audio source 0 is used (Visualizer(0); //system mix),
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
are needed. After adding the permissions to my app and installing the (new compiled) app again, my app still crashed. I found out, that the device has to be restarted, to use the Visualizer without any exception (for whatever reason). So if you develop an app and get this exception, a restart could be required after adding the permissions to the app .

Categories

Resources