Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing the data of another application

Tags:

android

I'm writing an application that should be able clear the private data of any other application. If you're wondering about the use case, its along the lines of an MDM/MAM client. I'd like to selectively wipe application data (vs. a full device wipe).

I came across the following API call in the Android source code.

ActivityManager.clearApplicationUserData(String packageName,IPackageDataObserverobserver)

The odd part is, that this is not really available to you as part of the SDK . (So eclipse will give you hell for trying to use it). However, it is present (see here),you can invoke it via reflection. I'm still however, unable to get hold of the IPackageDataObserver interface.

Is there a better way of doing this? I know it CAN be done since I've seen products like MaaS360 do a selective wipe of applications' data.

Any suggestions?


UPDATE

Based on what @lechlukasz has outlined below... the following code can execute...but you do finally land up with a SecurityException, since the package manager revokes the CLEAR_APP_USER_DATA permission when the app is installed.

Class<?> iPackageDataObserverClass= Class.forName("android.content.pm.IPackageDataObserver");

Class<ActivityManager> activityManagerClass=ActivityManager.class;
ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

Method clearDataMethod=activityManagerClass.getMethods()[0];

Object iPackageDataObserverObject = Proxy.newProxyInstance(
    MyApp.class.getClassLoader(), new Class[]{iPackageDataObserverClass}, 
                        new InvocationHandler() {

            public Object invoke(Object proxy, Method method, Object[] args) 
                    throws Throwable {
                Log.i("Proxy", method.getName() + ": " + Arrays.toString(args));
                return null;
            }
        });


clearDataMethod.invoke(activityManager, "com.example.test",iPackageDataObserverObject);

So this works, insofar as the method can be called. No luck on actually being able to clear the data itself. :-(

like image 249
Archit Avatar asked Feb 22 '12 07:02

Archit


1 Answers

The method you point isn't static method, so in order to call it you would need the ActivityManager instance, which would be the trickiest part, even if you have root privileges. I can't help you with that.

But as for instantiating IPackageDataObserver, I've managed to do this without special privileges, using standard refrection API:

        Class ipdoClass = Class.forName("android.content.pm.IPackageDataObserver");
        Object observer = Proxy.newProxyInstance(
                MyApp.class.getClassLoader(), new Class[]{ipdoClass}, 
                        new InvocationHandler() {

            public Object invoke(Object proxy, Method method, Object[] args) 
                    throws Throwable {
                Log.i("Proxy", method.getName() + ": " + Arrays.toString(args));
                return null;
            }
        });
like image 107
Danubian Sailor Avatar answered Oct 22 '22 21:10

Danubian Sailor