Unable to add window -- permission denied when trying to create floating view - java

I am currently looking to develop an interface debug tool for Android which would display some system information overlaid on top of whatever app is running underneath. I've look at many different resources online and followed the basic procedure of setting my application to TYPE_APPLICATION_OVERLAY defining the permissions required in my manifest with android.permission.SYSTEM_ALERT_WINDOW, however, when trying to run the application the process, it crashes with the error FATAL EXCEPTION ... Unable to add window android.view.ViewRootImpl$W... -- permission denied for window type 2038.
I tried further investigating what could be causing the problem, and I managed to find out that since a certain version, Android will require for users to explicitly set permissions for apps to draw over other apps via Settings.ACTION_MANAGE_OVERLAY_PERMISSION, however, even after enabling permissions to draw over other apps, the process still resulted in the same error.
I currently have my app setup to check whether draw-over permissions have been granted, and asks the user to set it if not. Once it confirms permissions are enabled by the user, it calls the service where I attempt to create an overlaying view.
I'm relatively new to Android development so I would appreciate any ideas or guidance!
Update: Managed to get it working by setting the window's LayoutParam to use TYPE_PHONE instead of TYPE_APPLICATION_OVERLAY. This is working for Android N, however it may be good to note that this method is deprecated for Android O+

Add this permission in your manifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Related

WRITE_SETTINGS permission Android

I am making a flash light app. So naturally most devices now a days have a built in flash for the camera but when a flash is not present I want to use the screen as the flash light. To make this functionality work I want to adjust the screen brightness so it can act as the flashlight.
To change the brightness I used code from this post. The code works fine and I can change the brightness without issue. As a side note this code I used works on Physical android 8.1, 10 and 11 devices.
My first question comes from a lint warning I get when adding the <uses-permission android:name="android.permission.WRITE_SETTINGS"/> permission. The warning says
"Permission is only granted to system apps". Now I can obviously just suppress this warning, but my worry is when I upload the app to the app store Google will either reject the AAB or the app will no longer be a "System app" once its downloaded from the app store. Will suppressing the error do anything?
My second question is is this a false positive? Looking at the manifest permission docs WRITE_SETTINGS has no special permission besides for having to get the users consent buuuut WRITE_SECURE_SETTINGS definitely does. As per the docs for WRITE_SECURE_SETTINGS "Allows an application to read or write the secure system settings. Not for use by third-party applications." So could this be lint thinking WRITE_SETTINGS is WRITE_SECURE_SETTINGS?

Get current value of a system setting and change value if needed

I'm fairly new to Android and Java, but I'm actively learning by watching tutorials and asking questions. As of now, I am practicing by coding my own app in Android Studio. I want to start off with something a little bit tougher. So, let me explain what the app does for you guys. I want the end product to display a UI (obviously) with a button in the middle of the screen that says "Change Value". That's not the hard part for me though. I have the UI set up just to my satisfaction. However, the part that I am struggling with is the Java part. Through my research I couldn't find anything related to what I'm trying to accomplish.
What I want to do is check the value of a string located in the Global Table ("adb_enabled") of the Android Settings Database and if it is enabled ("1") then change it to ("0"). I've seen guides on how to change Brightness and get the current time, but they didn't help me that much.
Here is my code for reference:
Settings.Global.putInt( getContentResolver() ,
Settings.Global.LOCKSCREEN_AD_ENABLED , 0 );
Executing the code above I receive the following error: error: cannot find symbol variable LOCKSCREEN_AD_ENABLED. Does anyone know how I can fix the above error? Thanks!
You can make changes in System Settings if your App is System App. Non-system apps i.e. apps installed in data partition can not modify system settings. Non-system apps can open modify Setting only using Intent or System UI.
From official documentation:
Global system settings, containing preferences that always apply
identically to all defined users. Applications can read these but are
not allowed to write; like the "Secure" settings, these are for
preferences that the user must explicitly modify through the system UI
or specialized APIs for those values.
If your app is System App then you can use below code to make the changes(For System App only):
int adb = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
adb = Settings.Global.getInt(getApplicationContext().getContentResolver(),
Settings.Global.ADB_ENABLED, 0);
}
// toggle the USB debugging setting
adb = adb == 0 ? 1 : 0;
Settings.Secure.putInt(getApplicationContext().getContentResolver(),
Settings.Secure.ADB_ENABLED, adb);
Add below permissions to AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>

How can I force touch events on none routed Android devices

I would like to write an application which can programatically generate touch events on the phone.
I have already tried this methods:
adb : adb swipe and so on... It requires USB cable and connect the phone to pc
adb ON TCPIP: same
My problem with adb is to require so much presetting by user and I don't want to make the user to learn it. I have already made an application which use adb, but it's difficult for the user to set up properly :(
I have heard and I tried to sign my application with platform sign, but it's not good for me, because I would like to publish my app on android market and it is not a system application...
I would like to find a way to achieve this in a single application which can produce touch outside of the app (from background) and I would like to publish on market.
I have found this application: https://play.google.com/store/apps/details?id=com.teamviewer.quicksupport.samsung which can basically do this. Because when I click on my computer, it send a message to the phone and the phone make the touch on the screen. How and with what privileges can teamviewer do it?
Please give me some advice about it.
I would like to find a way to achieve this in a single application which can produce touch outside of the app (from background) and I would like to publish on market.
This is not possible, outside of what little input faking can be done by an accessibility service.
How and with what privileges can teamviewer do it?
If your read that Play Store listing, you will notice that it only works with Samsung devices. That is because the makers of TeamViewer struck a deal with Samsung to enable this sort of integration. Similarly, the TeamViewer team struck deals with a few other device manufacturers. However, they did not do so with all manufacturers, and so TeamViewer does not work on all devices.
Have a look here .This might be useful.
InputInjector
Android library that eases the process of injecting InputEvents (MotionEvent, KeyEvent) to you Android app. The library uses internal API calls to inject events and depend on the accessability of these. This library will therefore not work on all devices but theoretically support Android 2.3 and forward (API level 9-18+).
Androd 2.3 (API level 9-15)
In older versions of Android we envoke the same system calls as used by the Instrumentation framework.
Permission No special permission needs to be set.
Androd 4.1.2 (API level 16 and forward)
As of API level 16 we have access to the InputManager class. We use this as the basis for the input injection.
Permission Using InputManager for injection requires setting permission android.permission.INJECT_EVENTS in your manifest.
Using this permission may require altering Lint Error Checking in order to be able to compile. In Eclipse this is done by going to Window->Preferences->Android->Lint Error Checking and then finding ProtectedPermissions and setting severity to something else than error.
NOTE: In order to inject events to other apps using InputManager, your apk must be signed with system level certificate.
https://github.com/arnebp/android-inputinjector

Android java set app permissions dynamically

Is it possible to set android app permission dynamically in java, usually it is set in AndroidManifest.xml. But what I want to do is giving my users a choice of what permissions they want my app to give.
thanks in advance!
No, it is not currently possible. Up through Android Lollipop, permissions must be explicitly requested in the app's manifest and must be collectively all granted or denied (not installing the app) by the user at install time.
Android M, however, is changing this. As of Android M, permissions will be granted at runtime instead of at install time, allowing you to give users more control over which permissions they allow your app to use.
See the Android M runtime permissions documentation for more detail.
At the moment it's not possible http://developer.android.com/guide/topics/security/permissions.html
Android has no mechanism for granting permissions dynamically (at run-time) because it complicates the user experience to the detriment of security.
Things may change in new android M release
Nope. App's permissions are declared in AndroidManifest.xml, which is used to tell Android system what permissions are required. App installer will show all permissions to help user judge the app is good or bad.
Thing about that if an malicious app require nothing at install-time and require camera permission at run-time dynamically
It would be against Android policy . The application has or not has the permissions for doing something . It can be a security problem if you can think of it.
It is possible no issue but in mid of app if not get then chances of getting crash in your app...

Modifying installed apps permissions (Android)

I want to develop an application for android to:
List installed applications in the phone.
Show a list of permissions that are granted for each app.
Have the ability to disable any permission granted to a specific app or enable it later on.
Till now I have done the first two points. I searched the web to know how to modify permissions but I couldn't find anything useful.
So can anybody give me a clue on how to implement this ? Because I think it is doable since there some apps that can modify permissions (e.g. Permission Manager).
There are various apps in the Play Store that call themselves Permission Manager so I'm not sure which one you have looked at.
At least one of them works by pulling the installed app apart, and reforming it with a changed AndroidManifest file (and since they don't have access to the original private key, a new signature). To run the reformed app you have to uninstall the old one, and install the new one, which has 2 significant downsides
all the data of the old app is lost
since you now have an effective pirate version of the app, the app will no longer update with new versions from the Play Store.
Other apps that claim to manage app permissions simply expose the hidden App Ops permission screen in Android 4.3, and Google removed this from the later Android 4.4 update.

Categories