and here is another example:
from the screenshot above we see user is able to disable picture in picture mode. you can find it in the "special app access" screen on the emulator api 27 . How can i detect if user has disabled this feature ?
i tried checking the following but it does not work:
packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
compiler states that AppOpsManager cannot be found. any ideas ?
just like AlexTa said. but i wanted to actually write the code to save someone some time:
private fun hasPermission(): Boolean {
val appOps = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
} else {
return false
}
return appOps.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
}
Try AppOpsManager.checkOp (String op, int uid, String packageName), where op is OPSTR_PICTURE_IN_PICTURE operation. That method should return MODE_ALLOWED constant if supports Picture in Picture operation.
For more info, check this link.
I might be late but here's the answer
private fun hasPermission(): Boolean {
val appOps = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager?
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
appOps?.unsafeCheckOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
} else {
appOps?.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
}
} else {
false
}
}
Related
I have a function that checks if developer mode is enabled or not, as the suggestion here:
Android - How to check if Developer option is enabled
Here is the code:
public boolean isDevMode() {
if(Build.VERSION.SDK_INT >= 17) {
return android.provider.Settings.Global.getInt(getApplicationContext().getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0) != 0;
} else {
return false;
}
}
It works perfectly on API 26+ but I've just tested it on the emulator on API 24 and it returns false regardless of if developer settings are enabled or not.
What am I missing? Is it a different option for < 26?
Fixed it by changing the default value to true for builds under oreo only.
public boolean isDevMode() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return Settings.Secure.getInt(context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
} else {
return Settings.Secure.getInt(context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1) != 0;
}
}
I need to check if my dialer app is default dialer app or not. Currently I'm using this code
private boolean isDefault() {
Intent i=new Intent(Intent.ACTION_DIAL);
i.addCategory(Intent.CATEGORY_DEFAULT);
ResolveInfo info=getPackageManager().resolveActivity(i,0);
CharSequence name=getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo);
Log.d(TAG, "isDefault: "+name);
return name==getString(R.string.app_name);
}
But name is always "Android System"
Your help will be greatly appreciated. Thanks
I was able to solve my problem by using TelecomManager here is my code
private boolean isDefault() {
TelecomManager manger= (TelecomManager) getSystemService(TELECOM_SERVICE);
String name=manger.getDefaultDialerPackage();
Log.d(TAG, "isDefault: "+name);
return name.equals(getPackageName());
}
Here's a safe way to do it, which will work for all Android versions:
fun isDefaultDialer(context: Context, packageNameToCheck: String = context.packageName): Boolean {
val dialingIntent = Intent(Intent.ACTION_DIAL).addCategory(Intent.CATEGORY_DEFAULT)
val resolveInfoList = packageManager.queryIntentActivities(dialingIntent, 0)
if (resolveInfoList.size != 1)
return false
return packageNameToCheck == resolveInfoList[0].activityInfo.packageName
}
Sample usage:
Log.d("AppLog", "is current app the default dialer?${isDefaultDialer(this)}")
Log.d("AppLog", "is Google Dialer the the default dialer?${isDefaultDialer(this,"com.google.android.dialer")}")
Telecom Manager's default dialer package is available from API level 23.
private fun getDefaultDialerApp() : String?{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val manager = applicationContext.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return manager.defaultDialerPackage;
}
}
return null //Change it based on your requirement.
}
Our goal is to use AndroidTest(AndroidJUnit4) to run automatic tests.
We have used this code to grant permissions when SDK >= 23
public static void grantPermission(String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermission(permission))
getInstrumentation().getUiAutomation()
.executeShellCommand("pm grant " + getTargetContext().getPackageName() + " " + permission);
}
}
#Before
public void setUp() throws Exception {
grantPermission("android.permission.ACCESS_NETWORK_STATE");
grantPermission("android.permission.CHANGE_NETWORK_STATE");
grantPermission("android.permission.CAMERA ");
grantPermission("android.permission.INTERNET");
grantPermission("android.permission.READ_EXTERNAL_STORAGE ");
grantPermission("android.permission.WRITE_EXTERNAL_STORAGE");
grantPermission("android.permission.PACKAGE_USAGE_STATS");
}
The method is fine for
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.CAMERA
Although in the Settings/Security/Apps with usage access, our app is on, but when we use this code to check permission, it still doesn't have that permission.
AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
// return MODE_DEFAULT, not MODE_ALLOWED
int result = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName())
How can I do to succeed this goal?
We have tried many approved. Finally, we succeeded.
The approved was use "adb shell input tap [x], [y]" to simulate tap, so it is like human tap the screen to trigger the switch.
This is the code us use.
private void grantPermission() throws InterruptedException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Context context = InstrumentationRegistry.getTargetContext();
final AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
// check if the app doesn't have permission
if (appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()) != AppOpsManager.MODE_ALLOWED) {
UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
//Open UsageAccessSettingsActivity
automation.executeShellCommand("am start com.android.settings/.Settings$UsageAccessSettingsActivity");
Thread.sleep(1000);
//Open the setting of the first app
automation.executeShellCommand(String.format("input tap %s %s", dpToPx(100), dpToPx(138)));
Thread.sleep(1000);
//Tap permit usage access
automation.executeShellCommand(String.format("input tap %s %s", dpToPx(100), dpToPx(164)));
Thread.sleep(1000);
}
}
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
We have tried 6 AVDs on SDK 23 and 24 with hdip, xhdip and xxhdip. All works.
UPDATED[2/10]:
We found another easier way to do it. use "adb shell appops" commend.
This is ours code.
#Before
public void setUp() throws Exception {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Context context = InstrumentationRegistry.getTargetContext();
final AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
if (appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()) != AppOpsManager.MODE_ALLOWED) {
InstrumentationRegistry
.getInstrumentation()
.getUiAutomation()
.executeShellCommand("appops set " + context.getPackageName() + " android:get_usage_stats allow");
}
}
}
Make sure your app has usage stats enabled. You can check this by launching the Usage Stats Activity. add this to your code on your onCreate method or possible on a button click:
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
I'm using the new UsageStatsManager API to get current foreground application in Android 5.0 Lollipop.
In order to use this API, the user must enable the application in the Settings->Security->Apps with usage access screen.
I send the user directly to this screen with this Intent:
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
Now, I want to validate the user enabled my application.
I wanted to do so like I validate the user enabled my application to use the NotificationListenerService but I have no idea what is the String key, if it even exists.
Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
// Tried Settings.ACTION_USAGE_ACCESS_SETTINGS as key but it returns null
Second approach was to query the usage stats and check if it returns results (it returns an empty array when the app is not enabled) and it works most of the times but sometimes it returns 0 results even when my app is enabled.
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
long time = System.currentTimeMillis();
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
if (stats == null || stats.isEmpty()) {
// Usage access is not enabled
}
Is there a way to check if my application has usage access enabled?
Received a great answer by someone on Twitter, tested working:
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
return (mode == AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
I previously used the same code as Bao Le, but I've run into the problem that certain devices (e.g. VF-895N) report usage stats as enabled even when they're not. As a workaround I've modified my code like this:
public static boolean hasPermission(#NonNull final Context context) {
// Usage Stats is theoretically available on API v19+, but official/reliable support starts with API v21.
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
return false;
}
final AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
if (appOpsManager == null) {
return false;
}
final int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
if (mode != AppOpsManager.MODE_ALLOWED) {
return false;
}
// Verify that access is possible. Some devices "lie" and return MODE_ALLOWED even when it's not.
final long now = System.currentTimeMillis();
final UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
final List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, now - 1000 * 10, now);
return (stats != null && !stats.isEmpty());
}
Successfully tested on multiple devices.
Here's my all-around solution for this (based on similar question and answer here) :
public static PermissionStatus getUsageStatsPermissionsStatus(Context context) {
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)
return PermissionStatus.CANNOT_BE_GRANTED;
AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
final int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
boolean granted = mode == AppOpsManager.MODE_DEFAULT ?
(context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED)
: (mode == AppOpsManager.MODE_ALLOWED);
return granted ? PermissionStatus.GRANTED : PermissionStatus.DENIED;
}
public enum PermissionStatus {
GRANTED, DENIED, CANNOT_BE_GRANTED
}
Detecting when the usage access changes
Use this class to be notified when your app is granted or revoked usage access.
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class UsagePermissionMonitor {
private final Context context;
private final AppOpsManager appOpsManager;
private final Handler handler;
private boolean isListening;
private Boolean lastValue;
public UsagePermissionMonitor(Context context) {
this.context = context;
appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
handler = new Handler();
}
public void startListening() {
appOpsManager.startWatchingMode(AppOpsManager.OPSTR_GET_USAGE_STATS, context.getPackageName(), usageOpListener);
isListening = true;
}
public void stopListening() {
lastValue = null;
isListening = false;
appOpsManager.stopWatchingMode(usageOpListener);
handler.removeCallbacks(checkUsagePermission);
}
private final AppOpsManager.OnOpChangedListener usageOpListener = new AppOpsManager.OnOpChangedListener() {
#Override
public void onOpChanged(String op, String packageName) {
// Android sometimes sets packageName to null
if (packageName == null || context.getPackageName().equals(packageName)) {
// Android actually notifies us of changes to ops other than the one we registered for, so filtering them out
if (AppOpsManager.OPSTR_GET_USAGE_STATS.equals(op)) {
// We're not in main thread, so post to main thread queue
handler.post(checkUsagePermission);
}
}
}
};
private final Runnable checkUsagePermission = new Runnable() {
#Override
public void run() {
if (isListening) {
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, Process.myUid(), context.getPackageName());
boolean enabled = mode == AppOpsManager.MODE_ALLOWED;
// Each change to the permission results in two callbacks instead of one.
// Filtering out the duplicates.
if (lastValue == null || lastValue != enabled) {
lastValue = enabled;
// TODO: Do something with the result
Log.i(UsagePermissionMonitor.class.getSimpleName(), "Usage permission changed: " + enabled);
}
}
}
};
}
Credits
Based on code from epicality in another answer.
This is an alternative solutions:
AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
android.os.Process.myUid(), context.getPackageName());
return mode == AppOpsManager.MODE_ALLOWED;
This works down to KitKat (API 19)
AppOpsManager appOps = (AppOpsManager) context
.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow("android:get_usage_stats",
android.os.Process.myUid(), context.getPackageName());
boolean granted = mode == AppOpsManager.MODE_ALLOWED;
None of the answer worked for me so i made this
public boolean permissiontodetectapp(Context context) {
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
return ((AppOpsManager) context.getSystemService(APP_OPS_SERVICE)).checkOpNoThrow("android:get_usage_stats", applicationInfo.uid, applicationInfo.packageName) != 0;
} catch (PackageManager.NameNotFoundException unused) {
return true;
}
}
this code working in lollipop and marshmallow i used this code in my app
if (Build.VERSION.SDK_INT >= 21) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
if (stats == null || stats.isEmpty()) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_USAGE_ACCESS_SETTINGS);
context.startActivity(intent);
}
}
If they are using an Amazon Fire tablet (and possibly other Fire OS devices) the user can download the application from a user installed Google Play Store then not have the option you want activated available in their OS. I know this because as a Fire OS user this happened to me a few minutes ago. Detecting whether a user has Fire OS and, if so, offering an option which actually exists would be fantastic for both user and dev.
try this ,
public boolean check_UsgAccs(){
long tme = System.currentTimeMillis();
UsageStatsManager usm = (UsageStatsManager)getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> al= usm.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, tme - (1000 * 1000), tme);
return al.size()>0;
}
I need to check whether "Automatic date and time" in the android device is enabled or not. If it is not enabled I need to display the popup that it is not enabled.
Is it possible to achieve. If possible, how to check enabled or not ?
Link for API 17 and above
android.provider.Settings.Global.getInt(getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0);
Link for API 16 and below
android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0);
public static boolean isTimeAutomatic(Context c) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
} else {
return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
}
}
I would just like to point out that it is possible to cheat (I just did it on a Samsung Galaxy S4, Android 5.0.1):
Disable auto time
Disconnect from the internets (airplane mode, no wifi etc...)
reboot phone (otherwise the real time is retrieved)
set auto time on
Done today:
Unix time: 1129294173
Date: 14102005024933
is automatic? 1 (using android.provider.Settings.Global.getInt(getActivity().getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0))
And today definitely isn't Oct 14, 2005
if(Settings.Global.getInt(getContentResolver(), Global.AUTO_TIME) == 1)
{
// Enabled
}
else
{
// Disabed
}
Check is Auto Time or Zone is Enabled ? (Kotlin)
fun isAutoTimeEnabled(activity: Activity) =
Settings.Global.getInt(activity.contentResolver, Settings.Global.AUTO_TIME) == 1
fun isAutoTimeZoneEnabled(activity: Activity) =
Settings.Global.getInt(activity.contentResolver, Settings.Global.AUTO_TIME_ZONE) == 1
in kotlin you can use
fun isTimeAutomatic(c: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.Global.getInt(c.contentResolver, Settings.Global.AUTO_TIME, 0) == 1
} else {
Settings.System.getInt(c.contentResolver, Settings.System.AUTO_TIME, 0) == 1
}}