Android, Change GPS status when app is device administrator enabled - java

How to set GPS status on when the app is set as Device administrator by user.
I'm using this method :
private void turnGPSOn() {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
getApplicationContext().sendBroadcast(intent);
String provider = Settings.Secure.getString(getApplicationContext()
.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.getApplicationContext().sendBroadcast(poke);
}
}
and getting this error at least on API-21:
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=27737, uid=10464
please please please care about Device administrator permission that is enabled and don't tag the question as duplicated!

android.location.GPS_ENABLED_CHANGE intent can only be broadcasted by system apps as it is a protected broadcast(means your app should be either signed with systemsignature or it should be a system app). Even if your app is selected as Device Admin app by user, it does not mean it is eligible to use system features. Device Admin app will get access to features that are exposed by DevicePolicyManager class. Some of the global settings and secure settings can be controlled using DevicePolicyManger class on Lollipop and above.
Control Secure Settings
Control Global Settings

Related

Android - Protection from FRP bybass

How to add extra protection in my device owner application so that if someone tries bypassing the FRP they can't use the phone.
I have seen this in some devices. they show a notification 'Device managed by admin automatically resetting soon' like this:
screenshot1
screenshot2
The app that showed this was using 'Android Management API'. I read about this but what I understood is that I think they install their own app as device owner in the device to add the FRP policy. In that case I think I will not be able to install my device owner app (or is it possible?).
I am using this code and it adds FRP (asks for the required gMail address during device setup) but it can be easily bypassed by following some YouTube videos and after that device does not show above notification.
try {
Bundle bundle = new Bundle();
bundle.putString("factoryResetProtectionAdmin", 432112340987654321234);//google Id
devicePolicyManager.setApplicationRestrictions(adminComponentName, "com.google.android.gms", bundle);
// send broadcast
Intent broadcastIntent = new Intent("com.google.android.gms.auth.FRP_CONFIG_CHANGED");
broadcastIntent.setPackage("com.google.android.gms");
broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
getApplicationContext().sendBroadcast(broadcastIntent);
} catch (Exception e) {
Log.d("FRP", e.getLocalizedMessage());
e.printStackTrace();
}

NFC is on, but adapter is not enabled

I am working on android project, where NFC is used as a communication. I am facing a weird problem, when mobile device has a NFC, it is enabled, but it is not working on some devices (adapter is not enabled when debugging). I am writing logs and it prints, NFC on, adapter disabled.
For example: HTC One m9(os 7.0). Also happens with OnePlus One(os 9)! But again, it works on other devices.
Did you experience the same issue?
Here is some code:
object NfcUtil {
fun getNfcAdapter(c: Context): NfcAdapter? {
val manager = c.getSystemService(Context.NFC_SERVICE) as NfcManager
return manager.defaultAdapter
}
fun doesSupportHce(c: Context): Boolean {
return c.packageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
}
}
val adapter = NfcUtil.getNfcAdapter(this)
if (adapter != null && NfcUtil.doesSupportHce(this)) {
if (adapter.isEnabled) {
tvNfcOff.extHide()
} else {
tvNfcOff.extShow()
}
}
I think that if NFC is supported and enabled but the adapter is disabled (https://developer.android.com/reference/android/nfc/NfcAdapter#isEnabled()) I'll follow the guidelines and redirects the user to the settings screen with the intent mentioned in the documentation.
If the user come back few times you could monitor it and show a different message instead of redirecting to settings, something like: NFC is not working properly on your device. I'd check if you have lots of users using those devices, if yes, I will try to research more on the Operating System and Device having this issue.
And later on I will just try to debug it with that Device and that specific Operating System that is having this kind of issue. I'll try to see if other apps using NFC has same issues or they work fine, and by work fine I mean that the communication happens not that other apps dont show any warning/error popup message.
And if I found out its an issue in a specific OS Version, also with other apps, I'll just try to inform the users and get an update on which version the issue have been fixed. Otherwise if other apps can make a successful NFC communication in that device/OS that is not working for me, I'll just dig deeper.
For now I can say there is nothing wrong in your implementation and looks good.
It might be an issue with the current OS or if you have any Custom ROM that might not fully support or have a functional NFC driver.
Two additional bits of info that might be useful
1) Use a Broadcaster receiver to get notified when the NFC state changes, because using the quick settings pull down does not pause your app, therefore retesting nfc status in onResume does not work (a user changing via the full settings app will pause you App, though)
Example of how to do it in Java
#Override
protected void onCreate(Bundle savedInstanceState) {
// All normal onCreate Stuff
// Listen to NFC setting changes
this.registerReceiver(mReceiver, filter);
}
// Listen for NFC being turned on while in the App
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF);
switch (state) {
case NfcAdapter.STATE_OFF:
// Tell the user to turn NFC on if App requires it
break;
case NfcAdapter.STATE_TURNING_OFF:
break;
case NfcAdapter.STATE_ON:
// Do something with this to enable NFC listening
break;
case NfcAdapter.STATE_TURNING_ON:
break;
}
}
}
};
2) Don't assume that the device has a NFC settings page, if your app works with and without NFC, if the adapter is null don't assume you can start an Intent to the NFC settings page as suggested by #denis_lor as this will cause a crash if the OS does not have a NFC adapter to turn on.

Android data turning on android data settings on button click

I have an app that does a check if data services is turned on and if it is not turned let display and alert dialog demanding the user to turn the app on clicking ok. This is my code that is supposed to show the settings of the android phone
final Intent intent=new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.phone","com.android.phone.Settings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
The above code does not display the option to turn on the wifi of the user
Please why the wifi data not turned on, on clicking the button
To enable the option to allow the user to turn on data services such wifi and others.
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
Just call the generic Settings screen (ACTION_SETTINGS)
Reference link http://developer.android.com/reference/android/provider/Settings.html
That will do the trick

How to use UsageStatsManager on Android Wear

I am trying to use UsageStatsManager on an Android Wear device.
What I did:
I am requesting the permission for it on the mobile app that is running on my phone. After the permission was given by the user, I can access the usage stats data.
Then I created a UsageStatsManager on the wear app and tried to access the usage stats data there. However, on the watch, I do not have the permission to do that and I can not ask for permission. To check if the permission was given by the user I used:
private boolean isPermissionGranted(){
try{
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) 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;
}
}
This returns true on the phone but false on the watch.
The problem:
I can ask the user for permission to use UsageStatsManager on the phone but I can not do that on the watch because the setting does not exist. Has anyone figured out if it is possible to get UsageStatsManager running on a watch?
Usage Permission intent isn't possible on Wear.
Same goes for Overlay (Settings.canDrawOverlays()) though at least you can use lower target to overcome it.
Also with latest Android N emulator this isn't resolved.

How to redirect User from Gallery TO my application in ANdroid?

When user taps gallery button I want user to redirect to my application's activity which will ask the user to enter password and then redirect user back to gallery ..
I want to know which permissions should i use and which broadcast should i accept .
Well If you want to lock the gallery you can do it in encrypt Decrypt way...
You can use MediaStore to access the images and then encrypt using your application and when user Again want to access that you can allow him to show by decrypting ...
Read the following link
Can I create password protected folder in Android?
You would need to replace the gallery application entirely to do what you're proposing.
When a user taps the Gallery icon, it doesn't fire a broadcast. It sends a directed intent with the action set to MAIN.
Anything that anyone tells you for how to do this is going to be wrong. You cannot do it.
The closest you could get is to use the MediaStore in your own app to access the same images that the gallery accesses.
Start a thread from your app which should periodically monitor foreground app name which in this case should be the Gallery (I cant remember the exact name But you can find it out using below logic).
Then if it is Gallery, open up your activity by blocking it.
If password success close activity & background will be the Gellery or otherwise redirect to Home.
For monitor foreground app
try {
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);
PackageManager pm = getApplicationContext().getPackageManager();
output1 = pm.getApplicationLabel(pm.getApplicationInfo(runningTasks.get(0).baseActivity.getPackageName(),PackageManager.GET_META_DATA)).toString();
String className = runningTasks.get(0).topActivity.getClassName();
if( className.contains("Gallery")) {
//better to create this object globally outside the thread.
Intent intentSettingLock = new Intent();
intentSettingLock.setClass(oContext,SettingsLockNew.class);
intentSettingLock.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
oContext.startActivity(intentSettingLock);
}
}
catch(Exception e) {
}
To redirect to home on invalid password
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
this.finish();
You can not perform this as you described because pressing the gallery app icon from the launcher the system fires an intent with action=MAIN and category=LAUNCHER for the specific application package.
Since you cannot name your application package as the gallery package name you cant filter this intent.

Categories