Android suppress ask permission for USB device - c#

I know this has been asked as found USB Device Access on S/O. Additionally finding resource of USB Host connectivity. And even context Activity Declaration in Android Manifest here, so I have looked.
To clarify, this is to support a USB SmartCard Reader device attached to an Android device.
I am trying to create a stand-alone library that can be integrated with another Xamarin/Android application, but not providing full source to the library itself. That said, I created a new Solution "TestXamLibs". In it are the default two projects...
TestXamLibs
TestXamLibs.Android
My additional separate project that will be used as a Resource library (for context) is
XamCACReader
Having said that, the basic premise to summarize the others. I have a Xamarin/Android application that will utilize a USB device that can get attached and removed. The app itself which is not much more than a skeleton app of opening screen and a BroadcastReceiver to handle the listening and handling when such device is attached/detached.
When attached, I have to keep requesting permission to use the USB device over and over.
These other web posts identify how to handle it so you dont have to keep responding... YES, allow it again by applying some sugar within the application's manifest.
While trying to add that component, the program wont run. Remove it, and it runs. HOPEFULLY, something stupid simple. One additional caveat, I am working in Visual Studio and see the "Resources" folder in the project, but so many other places refer to the "res" folder explicitly and dont know if there is something else I am missing, or just because of the IDE of Visual Studio vs other Android based IDE.
Anyhow, below is the entire manifest including what I TRIED to implement to stop the nag prompting.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.companyname.testxamlibs">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="TestXamLibs.Android"
android:theme="#style/MainTheme"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<activity>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#drawable/MyUsbDevice" />
</activity>
</application>
</manifest>
Then, in the Resources\Drawable folder, I added the "MyUsbDevice.xml" and its content below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="2278" product-id="13367" />
</resources>
Now, based on above, manifest, as it exactly sits above, when I try to run, I get a failure of
Severity Code Description Project File Line Suppression State
Error Missing 'name' key attribute on element activity at AndroidManifest.xml:34:5-39:16 TestXamLibs.Android
Another time I tried and had more information, but still did not work, it was nagging about
android:debuggable must be set to true
and obviously not able to debug.
Aside from needing the assistance to get this manifest correct, is there some other way directly within code that can bypass what this manifest is allowing otherwise? As having seen all the posts about same context, you might think there was another way.

Related

Cannot set "User Notification Listener" capablity in visual studio for UWP app

I'm trying to follow this guide on setting up a notification listener in a unified windows platform (UWP) application. I've got the example code running up until listener.RequestAccessAsync is called, but it keeps returning UserNotificationListenerAccessStatus.Denied without asking me to allow the application to access that information (it also never asked me before and I cannot find the app in ms-settings:privacy-notifications.
I've tried to set the capabilities of the app to allow "User Notification Listener" like it says in the first paragraph of the above mentioned page, but that does not appear in the list of available capability settings in my Package.appxmanifest. The target and minimal supported version for the project solution is 16299.
Is there a way to make this work or has this broken since this feature was added in windows build 14393?
You'll need to manually add this capability to the manifest. Right-click on the Package.appxmanifest in the Solution Explorer and select View Code. Make the following changes to the file:
<Package ...
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
IgnorableNamespaces="... uap3">
...
<Capabilities>
...
<uap3:Capability Name="userNotificationListener"/>
</Capabilities>
</Package>

Applying xml transformation to app.manifest

I just inherited a c# application. It currently has an entry in it's app.manifest to enable UAC
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Every time I do a debug build it inside visual studio, I get prompted that "This task requires the application to have elevated permissions". (I have an admin account but I don't logon with it when developing.)
Is there a ways to either apply a xml transformation to it (like on web.configs) or making a app.manifest for release mode?
Using the SlowCheetah NuGet package and accompanying Extension you will get the same behavior on all xml files as you have for web.config.
Be sure to install/activate the NuGet package as well as the Visual Studio Extension. Also, there are a number of Slow Cheetah versions in NuGet - I would suggest using the latest that is released by Microsoft - Microsoft.VisualStudio.SlowCheetah.
Read more on this: https://github.com/Microsoft/slow-cheetah
Edit: I had a long struggle actually getting the transform to work for App.Manifest.xml for my sharepoint add-in project. Turns out the files created for you when you use "Add transform" lack some details that if not included will cause the transform to fail (give no result). This is what I concluded:
<!-- Mandatory block in AppManifest transform files: -->
<App xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest"
Name="Not transformed"
ProductID="{12345678-0000-0000-0000-123456789123}"
Version="0.0.0.0"
SharePointMinVersion="0.0.0.0"
>
<Properties>
<Title>Not transformed</Title>
<StartPage>Not transformed</StartPage>
</Properties>
<AppPrincipal>
<RemoteWebApplication ClientId="*" />
</AppPrincipal>
</App>
<!--
This block as it is written will cause no transformation whatsoever, but all elements above must be present for any transformation to be applied.
To transform an entire element along with its text content, add the attribute xdt:Transform="Replace" to the element. This will also replace all
child elements.
-->
Hope this is of help!

Xamarin.Android: Problems changing the application icon from the default icon

I just started learning how to develop for Android a few days ago, so I'm still in the process of learning all the ins and out of the environment. Recently, I finished writing the functionality for a basic Contacts app and decided to turn to fixing some of the finer points of the UI. I have been trying for the last couple of days to change the launcher icon for the app from the default Android icon Default Icon to one that I selected Selected Icon. However, nothing I seem to try will allow the icon I selected to appear on the device I am testing on.
I added what is supposed to be the proper line in the Android Manifest file as seen here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="Contact_Manager.Contact_Manager" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:label="Contacts" android:icon ="#drawable/contacts-3"></application></manifest>
But this solution did not work. I also tried deleting the default icon from the drawable folder(I also saved a copy of it elsewhere) but it still showed the default Icon on the device screen. I then tried to uninstall the application on the device in order to clear all the cache files, but was met with the same outcome. Is there something I'm doing wrong?
Note: I am also new to StackOverFlow so I apologize if the formatting of this question is awful.
I suspect you have not deleted all the versions of default icon. In android there is a icon for each screen density in drawable-mdpi, drawable-hdpi, etc folder. Hence search for that file and remove it from all the folders and try again.
You need to add fresh icons for each density.
UPDATE 1 :
I think I know the problem now. Right click the project on solution explorer. Then click on options. Then Build-Android Application.
Change the application icon there.
Delete the default icon.png files from all the Android drawable folders, and save your new icon as icon.png. Also make sure that in the manifest you point to the icon file:
<application android:label="<Your app name>" android:icon="#drawable/icon"></application>
Try with a different icon name
Copy the new icon(myIcon.png) into mipmap- folders.
Update the icon value in Android project MainActivity.cs file
Uninstall the application from your device and try again.
Had the same issue. Deleted the old icons (maybe you only removed them from project not deleted them? I dont know) and tried building. It threw errors wherever the old icon was referenced so I was able to trace the issue. Also removing the old and adding the new with the same name (Icon.png) worked for me

EventLog WriteEntry not does not write into EventViewer in C#

I have created a C# application which creates custom log name in the event viewer, and writing entries into event viewer.
My application is working fine on Windows-7 OS machine But when i copied my binaries into another window-2012 server standard(SP1) machine its not working.
I ran my application RunAs administrator and I am seeing only custom log name but not the logging messages.
I have given full permission in the registry eventlog entry but no luck.
Could somebody suggest me how to fix the issue. Or debugging technique to fix this issue.
I have debugged the code there are no exceptions, Code is running smoothly.
my code is so simple like as below:
EventLog.CreateEventSource("MyApp", "TestingApplication")
EventLog.WriteEntry("MyApp", "Testing 123")
today i have tested with same eventsource and eventlogname, like as below
EventLog.CreateEventSource("MyApp", "MyApp")
it worked fine in window-2012 server standard(SP1), is there anything issue in Win-2012(SP1) or am I missing anything.
Even while running as an administrator, in Windows Server 2008 R2 it defaults to not having elevated privileges. You need an elevated privilege to create the source. Once the source is created, it should let you write to it.
There are a few ways to solve this, if you're building an actual application, you can modify the application manifest to require elevated privileges and admin rights:
<?xml version="1.0" encoding="utf-8" ?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator"
uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
Or, again if you're building a normal application, you can split the code to create the event source into a separate process. Then you can check to see if the event source exists, if it does, just use it, if it does not, you can run your new process that creates the event source.
In the Process start info you'll need the 'runas' verb.
If you're running a windows service, you're a bit more restricted, as services do not have the capability of "elevating" privileges through their manifest directly as far as I'm aware.
However, your service installer can elevate just fine, and should be running under the needed admin rights, you can create your event source inside the service installer; and then just consume it within the service itself.
I solved this problem by Stop/Start Windows Event Log services. I dont know what was the problem but like Leo saied it can be a bug.
I was having exact same issue. I am using WinServer 2012r2. Log created successfully but writing events appear in the Application Log. I read a lot of MSDN and forums and done everything up to the books and still have this problem. Seems MS has a bug in latest .Net framework which causes all messages to go to the Application Log :(
Finally after I rebooted server everything was working fine. I am not sure why would I need to reboot server if testing with eventcreate command line everything was working fine.

Configuring Postsharp logging toolkit

Using the free version of Postsharp, I added a logging aspect (using the toolkit, didn't code it myself). Later I changed my mind and wanted to log only upon entering a function, and not on leaving.
Where can this be configured? Couldn't find it anywhere.
Thanks!
Diagnostics configuration is stored in solution (.pssln) or project level configuration file (.psproj). These files may not contain required configuration tags or even they may be missing by default.
You can open the configuration wizard from smart tag over any method without [Log] attribute. The configuration you are interested in is on the first page (Logging Level). Note that there is "New logging profile..." at the bottom of this page - you can have multiple configurations within one application.
If you change diagnostics configuration then pssln file is created after completing the wizard and it should contain something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration" xmlns:d="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics" xmlns:p="http://schemas.postsharp.org/1.0/configuration" xmlns:p1="http://schemas.postsharp.org/1.0/configuration">
<Property Name="LoggingEnabled" Value="{has-plugin('PostSharp.Patterns.Diagnostics')}" Deferred="true" />
<d:LoggingProfiles p:Condition="{$LoggingEnabled}">
<d:LoggingProfile Name="Default" OnExceptionLevel="None" OnSuccessLevel="None" />
</d:LoggingProfiles>
</Project>
The interesting tag is d:LoggingProfile with it's attributes OnExceptionLevel and OnSuccessLevel. You can add this configuration file manually as well.

Categories