I have used reflection to mount/unmount external storage.it is working below 4.4 Api.
code is below
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.storage.IMountService;
private static final String MOUNT_POINT = "/mnt/ext_usb" or "/mnt/sdcard/" ...
private IMountService mMountService = null;
private synchronized IMountService getMountService() {
if (mMountService == null) {
IBinder service = ServiceManager.getService("mount");
if (service != null) {
mMountService = IMountService.Stub.asInterface(service);
} else {
Log.e(TAG, "Can't get mount service");
}
}
return mMountService;
}
private void mount() {
IMountService mountService = getMountService();
try {
if (mountService != null) {
mountService.mountVolume(MOUNT_POINT);
} else {
//
}
} catch (RemoteException ex) {
// Not much can be done
}
}
private void unmount() {
StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
String state = sm.getVolumeState(MOUNT_POINT);
if (!Environment.MEDIA_MOUNTED.equals(state) &&
!Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
//
return;
}
IMountService mountService = getMountService();
try {
if (mountService != null) {
mountService.unmountVolume(MOUNT_POINT, true, false);
} else {
Log.e(TAG, "Mount service is null, can't unmount");
}
} catch (RemoteException ex) {
// Not much can be done
}
}
Any workaround to get it working.As it throws Security Exception.android.permission.mount_unmount_filesystems requires.I have decleared this in manifest.I have google about this issue i found that the permission have system|signature protection level.Thanks in advance.
In order to use something with signature | system permissions your package has to be signed by the platform's signing key. Unless you're creating your own custom ROM or have a rooted device, you won't be able to do this.
If your app is a regular 3rd party app (released in the Play store) then you should only use the public APIs and not depend on reflection. Only the public Android APIs are considered stable and exposed. Others are hidden because they are only intended to be used by the internals of the system.
Related
I'm try send DTMF codes in icoming CALL. For this i'n try use Java reflection:
public void initialize(){
ClassLoader classLoader = Dtmf.class.getClassLoader();
final Class<?> classCallManager = classLoader.loadClass("com.android.internal.telephony.CallManager");
Method methodGetInstance = classCallManager.getDeclaredMethod("getInstance");
objectCallManager = methodGetInstance.invoke(null);
methodGetState = classCallManager.getDeclaredMethod(SEND_DTMF, char.class);
}
public boolean sendDtmf(char ch) {
boolean result = false;
if ( methodGetState != null) {
try {
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
if (res instanceof Boolean) {
result = ((Boolean) res).booleanValue();
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
Link for source code of class CallManager : Call Manager source code
But i'm always get "false" in method sendDtmf(). In debug, code is go into next:
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
What wrong?
The method is likely throwing an InvocationTargetException if your application isn't signed with the platform certificate as conventional apps cannot execute these methods (and will not be granted the required platform permissions to do so).
In short: the method is returning false because you're catching (and ignoring) the exception.
There's an open issue (#1428) on the Android issue tracker for sending DTMF tones as it presently isn't possible.
I am trying to read WIFI proxy settings
Proxy host
Proxy port
Proxy user (authentication)
Proxy password (authentication)
from devices in android versions 2.X.X – 4.X.X without any success.
Calling:
String proxy = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.HTTP_PROXY);
Always returns null.
I've also added to my android manifest:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
still it returns null.
Also tried:
android.net.Proxy. getHost(Context ctx) – which is deprecated – returns the IP
android.net.Proxy. getPortt(Context ctx) – which is deprecated – returns always -1.
Java calls:
System.getProperty("http.proxyHost");
System.getProperty("http.proxyCall");
Also returns null.
Is there a working code which retrieves all these settings or at least partially from devices in all android versions?
I found this project: Android Proxy Library
Which provides backward compatible ways of querying Proxy settings as well as setting them for WebViews on older versions of Android.
// Grab Proxy settings in a backwards compatible manner
ProxyConfiguration proxyConfig = ProxySettings.getCurrentHttpProxyConfiguration( context );
// Set Proxy for WebViews on older versions of Android
ProxyUtils.setWebViewProxy( getActivity().getApplicationContext() );
However, there is something you need to understand about Proxy Settings set on a WiFi AP. Since WiFi specific Proxy Settings were not implemented in Android proper until 3.1, all pre-3.1 devices that expose that functionality are using some sort of custom hack. They don't work in any sort of standard way. So libraries like this won't be able to grab any proxy set from one of those hacks.
There is however a System Wide Proxy in pre-3.1 that this sort of library WILL grab. Of course Android saw fit not to provide any official way to modify this setting. But there are apps on the Play Store that will allow you to do it, this is the one I'm using: Proxy Settings and it works well, setting the System Proxy and allowing you to grab it either via this library, or even simpler methods like querying the JVM proxy settings.
I ended up not using the APL and instead went with a much simpler implementation:
private static final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
...
String proxyAddress;
int proxyPort;
if( IS_ICS_OR_LATER )
{
proxyAddress = System.getProperty( "http.proxyHost" );
String portStr = System.getProperty( "http.proxyPort" );
proxyPort = Integer.parseInt( ( portStr != null ? portStr : "-1" ) );
}
else
{
proxyAddress = android.net.Proxy.getHost( context );
proxyPort = android.net.Proxy.getPort( context );
}
This is what I'm using:
public static String[] getUserProxy(Context context)
{
Method method = null;
try
{
method = ConnectivityManager.class.getMethod("getProxy");
}
catch (NoSuchMethodException e)
{
// Normal situation for pre-ICS devices
return null;
}
catch (Exception e)
{
return null;
}
try
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Object pp = method.invoke(connectivityManager);
if (pp == null)
return null;
return getUserProxy(pp);
}
catch (Exception e)
{
return null;
}
}
private static String[] getUserProxy(Object pp) throws Exception
{
String[] userProxy = new String[3];
String className = "android.net.ProxyProperties";
Class<?> c = Class.forName(className);
Method method;
method = c.getMethod("getHost");
userProxy[0] = (String) method.invoke(pp);
method = c.getMethod("getPort");
userProxy[1] = String.valueOf((Integer) method.invoke(pp));
method = c.getMethod("getExclusionList");
userProxy[2] = (String) method.invoke(pp);
if (userProxy[0] != null)
return userProxy;
else
return null;
}
Following is code snippet to retrieve proxy details
public static String getProxyDetails(Context context) {
String proxyAddress = new String();
try {
if (IsPreIcs()) {
proxyAddress = android.net.Proxy.getHost(context);
if (proxyAddress == null || proxyAddress.equals("")) {
return proxyAddress;
}
proxyAddress += ":" + android.net.Proxy.getPort(context);
} else {
proxyAddress = System.getProperty("http.proxyHost");
proxyAddress += ":" + System.getProperty("http.proxyPort");
}
} catch (Exception ex) {
//ignore
}
return proxyAddress;
}
It'll return enmpty if some exception or no proxy detected;
private fun getUserProxy(context: Context): Data {
return try {
val declaredField = WifiConfiguration::class.java.getDeclaredField("mIpConfiguration")
declaredField.isAccessible = true
val data =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager)
?.configuredNetworks
?.asSequence()
?.mapNotNull {
try {
declaredField.get(it)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
?.mapNotNull {
try {
(it.javaClass.getDeclaredField("httpProxy").get(it) as? ProxyInfo)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
?.find { !it.host.isNullOrEmpty() }
?.let { Data(it.host ?: "", it.port.toString()) }
?: Data()
declaredField.isAccessible = false
return data
} catch (e: Exception) {
e.printStackTrace()
Data()
}
}
data class Data(
val host: String = "",
val port: String = ""
)
I'm adapting an app so it will work on the KindleFire, which doesn't have a camera.
I don't have any Android devices that lack a camera, so I don't know if the following code actually will return false for the Kindle. I'm using reflection because my app has already been released with Donut compatibility, and Donut doesn't have PackageManager.hasSystemFeature().
I'm assuming Donut devices all have cameras--hasn't caused me trouble yet.
public static boolean isCameraAvailable(Context context){
PackageManager pm = context.getPackageManager();
return tryHasSystemFeature(pm,"android.hardware.camera");
}
private static Method packageManager_hasSystemFeature;
static {
initCompatibility();
};
private static void initCompatibility() {
try {
packageManager_hasSystemFeature = PackageManager.class.getMethod(
"hasSystemFeature", new Class[] { String.class } );
} catch (NoSuchMethodException nsme) {
//leave the Method null
}
}
static private boolean tryHasSystemFeature(PackageManager pm,String feature){
if (packageManager_hasSystemFeature != null) {
try {
final Boolean hasIt = (Boolean) packageManager_hasSystemFeature.invoke(pm,feature);
return hasIt.booleanValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return true;
}
Actually it's recommended to add this line in your manifest file, if you want to be sure the device has a camera:
<uses-feature android:name="android.hardware.camera" />
Market will prevent a device without a camera from downloading your application.
Things should be simple, but as most of the time, in Android, aren't. I need to format the SD card if the user selects the option in my app. Don't ask me why I need to do this if it's already in the OS... not practical but it's a requirement that I need to implement. As you may know, there is an option in Settings \ Storage \ Erase SD Card. I took a look at the froyo source code and it's something like:
final IMountService service =
IMountService.Stub.asInterface(ServiceManager.getService("mount"));
if (service != null) {
new Thread() {
public void run() {
try {
service.formatVolume(Environment.getExternalStorageDirectory().toString());
} catch (Exception e) {
// Intentionally blank - there's nothing we can do here
Log.w("MediaFormat", "Unable to invoke IMountService.formatMedia()");
}
}
}.start();
} else {
Log.w("MediaFormat", "Unable to locate IMountService");
}
It uses android.os.storage.IMountService and android.os.ServiceManager and I don't seem to have access to it. So, as I see it I could recursively search every file and delete it but that would be "not on my taste"... or I could start the screen from Erase SD card to the user.
Any help is more then welcome, as I am stuck.
First of all, I think that you may need to umount .android_secure filesystem before formatting SD card, whatever your approach may be.
Then,
Try including following permissions in your app:
1) MOUNT_FORMAT_FILESYSTEMS - http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_FORMAT_FILESYSTEMS
2) MOUNT_UNMOUNT_FILESYSTEMS - http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_UNMOUNT_FILESYSTEMS
Android Settings app already uses the 2nd permission.
================================================================================
When you perform a build of AOSP or any other distribution code, IMountService.java file gets generated automatically. It contains following function which actually sends formatting commands to vold daemon I guess.:
private static class Proxy implements android.os.storage.IMountService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
// **** A LOT OF OTHER CODE IS HERE.....
public int formatVolume(java.lang.String mountPoint) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(mountPoint);
mRemote.transact(Stub.TRANSACTION_formatVolume, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
I can't find again the question here on SO, but It had a working solution. So all credit goes to that guy ;)
public void wipeMemoryCard() {
File deleteMatchingFile = new File(Environment
.getExternalStorageDirectory().toString());
try {
File[] filenames = deleteMatchingFile.listFiles();
if (filenames != null && filenames.length > 0) {
for (File tempFile : filenames) {
if (tempFile.isDirectory()) {
wipeDirectory(tempFile.toString());
tempFile.delete();
} else {
tempFile.delete();
}
}
} else {
deleteMatchingFile.delete();
}
} catch (Exception e) {
Utils.log(e.getMessage());
}
}
private static void wipeDirectory(String name) {
File directoryFile = new File(name);
File[] filenames = directoryFile.listFiles();
if (filenames != null && filenames.length > 0) {
for (File tempFile : filenames) {
if (tempFile.isDirectory()) {
wipeDirectory(tempFile.toString());
tempFile.delete();
} else {
tempFile.delete();
}
}
} else {
directoryFile.delete();
}
}
I'm using aidl to answer call automagically, code as following:
ITelephony.Stub.asInterface(ServiceManager.getService("phone"))
.answerRingingCall();
I import ServiceManager.class
import android.os.ServiceManager;
but there's a problem:The import android.os.ServiceManager cannot be resolved
How can I make it work? Thanks
android.os.ServiceManager is a hidden class (i.e., #hide) and hidden classes (even if they are public in the Java sense) are removed from android.jar, hence you get the error when you try to import ServiceManager. Hidden classes are those that Google does not want to be part of the documented public API.
Applications using non-public API cannot be compiled easily, there will be different platform versions of this class.
Though it is old one, but no one has answered it yet. Any hidden classes can be used using reflection APIs. Here is an example to acquire a service using Service Manager via reflection APIs:
if(mService == null) {
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "My_SERVICE_NAME");
if(binder != null) {
mService = IMyService.Stub.asInterface(binder);
}
if(mService != null)
mIsAcquired = true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Service is already acquired");
}
As said above these methods work only on System apps or framework apps from Android N on words.
Still we can code for System app for ServiceManager usage as below using reflection of Android Code
#SuppressLint("PrivateApi")
public IMyAudioService getService(Context mContext) {
IMyAudioService mService = null;
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "YOUR_METHOD_NAME");
if (binder != null) {
mService = IMyAudioService .Stub.asInterface(binder);
}
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return mService;
}