-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PermissionGranter to Barista (#20)
* Add PermissionGranter based on InfoJobs' app * Add PermissionGranter's gradle dependencies * Use BaristaSleepActions to sleep, avoiding Thread.sleep() * Introduce PermissionGranter at README.md * Avoid appcompat-v7 dependency to avoid dependency issues with Barista users * Use Log.e instead of System.out * Fix a bad message in the Log.e * Override uiautomator version to enable Barista to work on projects below API 18 * Add the Exception to Log.e, to give more information about the issue * Share that PermissionGranter needs API 18
- Loading branch information
1 parent
84c93ec
commit 3ec70a9
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
<manifest package="com.schibsted.spain.barista"/> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.schibsted.spain.barista"> | ||
|
||
<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/> | ||
</manifest> |
54 changes: 54 additions & 0 deletions
54
library/src/main/java/com/schibsted/spain/barista/permission/PermissionGranter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.schibsted.spain.barista.permission; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.RequiresApi; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.uiautomator.UiDevice; | ||
import android.support.test.uiautomator.UiObject; | ||
import android.support.test.uiautomator.UiObjectNotFoundException; | ||
import android.support.test.uiautomator.UiSelector; | ||
import android.util.Log; | ||
|
||
import static android.support.test.InstrumentationRegistry.getInstrumentation; | ||
import static com.schibsted.spain.barista.BaristaSleepActions.sleep; | ||
|
||
@RequiresApi(18) | ||
public class PermissionGranter { | ||
|
||
private static final int PERMISSIONS_DIALOG_DELAY = 3000; | ||
private static final int GRANT_BUTTON_INDEX = 1; | ||
|
||
public static void allowPermissionsIfNeeded(String permissionNeeded) { | ||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(InstrumentationRegistry.getTargetContext(), | ||
permissionNeeded)) { | ||
sleep(PERMISSIONS_DIALOG_DELAY); | ||
UiDevice device = UiDevice.getInstance(getInstrumentation()); | ||
UiObject allowPermissions = device.findObject(new UiSelector() | ||
.clickable(true) | ||
.checkable(false) | ||
.index(GRANT_BUTTON_INDEX)); | ||
if (allowPermissions.exists()) { | ||
allowPermissions.click(); | ||
} | ||
} | ||
} catch (UiObjectNotFoundException e) { | ||
Log.e("Barista", "There is no permissions dialog to interact with", e); | ||
} | ||
} | ||
|
||
private static boolean hasNeededPermission(Context context, String permissionNeeded) { | ||
int permissionStatus = checkSelfPermission(context, permissionNeeded); | ||
return permissionStatus == PackageManager.PERMISSION_GRANTED; | ||
} | ||
|
||
private static int checkSelfPermission(@NonNull Context context, @NonNull String permission) { | ||
if (permission == null) { | ||
throw new IllegalArgumentException("permission is null"); | ||
} | ||
return context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid()); | ||
} | ||
} |