I am trying to clear the app cache of other android apps besides my own. To do this, I am using reflection on the PackageManager class. However, whenever I initialize the method before I invoke it, it always ends up being null.
private void initiateClearUserData() {
// Invoke uninstall or clear user data based on sysPackage
String thePackageName;
PackageManager pm = speedy.this.getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
ApplicationInfo ai;// = installedApps.get(0);
ActivityManager.RunningAppProcessInfo process;
for(int x=0; x<4; x++){
ai = installedApps.get(x);
Here is where my problem is:
thePackageName = ai.packageName.toString();// mAppEntry.info.packageName;
Method deleteApplicationCacheFiles = null;
mClearCacheObserver = new ClearCacheObserver();
try {
deleteApplicationCacheFiles = pm.getClass().getMethod(
"deleteApplicationCacheFiles", String.class, PackageManager.class);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(deleteApplicationCacheFiles!= null){
try {
deleteApplicationCacheFiles.invoke(thePackageName, mClearCacheObserver);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(speedy.this, "Hell naw",
Toast.LENGTH_SHORT).show();
}
}
}
Because Method deleteApplicationCacheFiles is null, my toast message shows up. Any suggestions?
Take a look at the docs for Security on Android: http://developer.android.com/guide/topics/security/security.html
A central design point of the Android security architecture is that no application, by default, has permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or e-mails), reading or writing another application's files, performing network access, keeping the device awake, etc.
It sounds like the system will block you from doing this (through reflection too).
Related
private static final long CACHE_APP = Long.MAX_VALUE;
private CachePackageDataObserver mClearCacheObserver;
btnCache.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clearCache();
}
});//End of btnCache Anonymous class
void clearCache()
{
if (mClearCacheObserver == null)
{
mClearCacheObserver=new CachePackageDataObserver();
}
PackageManager mPM=getPackageManager();
#SuppressWarnings("rawtypes")
final Class[] classes= { Long.TYPE, IPackageDataObserver.class };
Long localLong=Long.valueOf(CACHE_APP);
try
{
Method localMethod=
mPM.getClass().getMethod("freeStorageAndNotify", classes);
/*
* Start of inner try-catch block
*/
try
{
localMethod.invoke(mPM, localLong, mClearCacheObserver);
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* End of inner try-catch block
*/
}
catch (NoSuchMethodException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}//End of clearCache() method
private class CachePackageDataObserver extends IPackageDataObserver.Stub
{
public void onRemoveCompleted(String packageName, boolean succeeded)
{
}//End of onRemoveCompleted() method
}//End of CachePackageDataObserver instance inner class
class CachePackageDataObserver extends IPackageDataObserver.Stub {
public void onRemoveCompleted(String packageName, boolean succeeded) {
}//End of onRemoveCompleted() method
}
I am using Below permission
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
The above code works fine for devices < Marsh Mellow but when i run this code on
Marshmellow devices it causes exception. i read many post that say that CLEAR_APP_CACHE permission is system reserved in and not for third party. then how do Clean master and other apps clear internal caches of other apps in android. please help me to solve the exception so that i can clear the caches of third party apps progrmatically.
Thanks in Advance.
Starting from Android 6.0, permission level for CLEAR_APP_CACHE is signature|privileged. Normal Android apps cannot hold this permission. This permission is only granted if your app is signed with the firmware's signing key or you are installed on the privileged system partition.
Prior to Android 6.0, protection level for CLEAR_APP_CACHE was dangerous, so normal apps could request it in the manifest.
I have this spring boot java controller having code that utilizes the OpenKM document management API to search the document management system for documents and display results using Ajax, HTML, CSS and Jquery datatables on the front-end.
Due to the way the API was written, I cannot get a document object with its metadata in one call but will need to use an output of the first API operation's call as a filter for another API operation method in two nested for loops.
Additionally, I had to iterate the toString method of an API return object to retrieve the metadata information, as they were not accessible through the return object's properties.
The problem is the performance of this code. I would like to see if there is a way to optimize this code.
// Read the property or metadata to use in constituting the StoredDocument object
for (QueryResult queryResult : resultSet.getResults()) {
// Create a locally-scoped List<String>
List<String> listOfStoredDocumentProperties = new ArrayList<String>();
Document document = queryResult.getDocument();
String nodeId = document.getPath();
// Populate storedDocument object
storedDocument = new StoredDocument();
storedDocument.setAuthor(document.getAuthor());
storedDocument.setCreated(document.getCreated());
storedDocument.setLastModified(document.getLastModified());
storedDocument.setPath(document.getPath());
storedDocument.setPermissions(document.getPermissions());
storedDocument.setSize(document.getActualVersion().getSize());
storedDocument.setUuid(document.getUuid());
storedDocument.setVersionNumber(document.getActualVersion().getName());
// System.out.println(nodeId);
try {
listOfFormElement = okm.getPropertyGroupProperties(nodeId, documentVo.getGroupId());
int counterForTrackingDocDirectionPos = 0;
for (FormElement formElement : listOfFormElement) {
++counterForTrackingDocDirectionPos;
if (counterForTrackingDocDirectionPos == 4) {
String formElementString = formElement.toString();
// System.out.println("formElementString: " + formElementString);
System.out.println("name: " + formElement.getName());
System.out.println("formElement: " + formElement);
String transformedFormElementString = StringUtils.EMPTY;
try {
transformedFormElementString = formElementString.substring(0, formElementString.indexOf(", selected=true"));
// Read the string from a position that is 3 steps before the last position in the string.
transformedFormElementString = transformedFormElementString
.substring(transformedFormElementString.length() - 3, transformedFormElementString.length()).trim();
transformedFormElementString = transformedFormElementString.startsWith("=")
? transformedFormElementString.substring(1, transformedFormElementString.length()) : transformedFormElementString;
} catch (Exception ex) {
// To catch scenario where formElementString.indexOf(", selected=true") does not find the
// specified string. This happens when document direction is not set and therefore is
// selected=false for both the options IN and OUT.
transformedFormElementString = "NOT SET";
}
listOfStoredDocumentProperties.add(transformedFormElementString);
System.out.println("transformedFormElementString: " + transformedFormElementString);
} else {
String formElementString = formElement.toString();
String transformedFormElementString = formElementString.substring(formElementString.indexOf("value="),
formElementString.indexOf("data="));
// Remove the preceding 'value=' and the last 2 character-constituted string ", "
transformedFormElementString = transformedFormElementString.substring(6, transformedFormElementString.length() - 2).trim();
listOfStoredDocumentProperties.add(transformedFormElementString);
}
}
storedDocument.setCompanyName(listOfStoredDocumentProperties.get(0));
storedDocument.setProductLine(listOfStoredDocumentProperties.get(1));
storedDocument.setSubjectHeading(listOfStoredDocumentProperties.get(2));
storedDocument.setDocumentDirection(listOfStoredDocumentProperties.get(3));
storedDocument.setDocumentType(listOfStoredDocumentProperties.get(4));
storedDocument.setReferenceNumber(listOfStoredDocumentProperties.get(5));
storedDocument.setDate(ISO8601.parseBasic(listOfStoredDocumentProperties.get(6)).getTime().toString());
// Add the storedDocument object to the return list
listOfstoredDocuments.add(storedDocument);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DatabaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WebserviceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The solution for it is extending the REST API. In the professional edition, the REST API is extensible with plugins architecture https://docs.openkm.com/kcenter/view/okm-6.4/creating-your-own-rest-plugin-(-extending-rest-api-).html, in the community this option still is not present. The idea is to build a method from server side what provide the exact data what really you need, creating high-level methods.
I'm have created a java app that reads files from a FTP server.I have tested this app locally(with success) and now i have deployed this app to heroku.
This is a piece a my code :
public void CSVListing() {
String[] fnames = {"1","2","3","4"};
try {
try {
client.connect(host);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
//----------------------------------------
boolean login = false;
try {
login = client.login(user, pass);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (login) {
System.out.println("Login success...");
//boolean logout = false;
System.out.println("ready to work");
}
else {
System.out.println("Login fail...");
}
//----------------------------------------
System.out.println(client.printWorkingDirectory());
fnames = client.listNames();
System.out.println("FNAMES ARE" + fnames);
for(String s: fnames){
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......................................................
I have locally executed this code and it gives me a list of the files, but when I execute this on my heroku app online >>fnames = client.listNames();<< returns null according to my logs. I'm accessing the same FTP host in both executions (locally and online).
Can somebody help me and tell me where i'm wrong?
Did you check your permissions? Awhile ago I pass through something similar, the problem was that the user connecting to the FTP server didn't have permission to download files.
I have a requirement where i need to mock the com.sun.deploy.security.DeployManifestChecker and return null when accessing printWarningsIfRequired in that class. Since Deploy.jar is not in my build path, i couldnt directly mock it up. I'm looking at a way to implement it using Java reflection API. but i'm not sure how to invoke the mock method with the Class argument.
method.invoke(null, new Class[]{claz1}); is failing with NP exception.
Here is the code
Mockery context ;
final Class<?> claz1;
try {
Class mclaz = Class.forName("org.jmock.Mockery");
context = (Mockery) mclaz.newInstance();
claz1 = Class.forName("com.sun.deploy.security.DeployManifestChecker");
final Method method = mclaz.getDeclaredMethod("mock",
new Class[]{Class.class} );
method.invoke(null, new Class[]{claz1});
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Your stated goal in to use reflection but if I had to do this i'd just create a test-only jar that includes an interface that mimics the com.sun.deploy.security.DeployManifestChecker class. Then use jmock to mock the stand-in.
I've taken this approach before when I couldn't include the actual class in my testing setup like you and it works like a champ. Just be sure that your stand-in doesn't make it to the runtime classpath and remains in test scope only.
I am new to J2ME. I want to play an audio song in my application. I have written
Player p = null;
try {
p = Manager.createPlayer(getClass().getResourceAsStream("aa.wav"),"audio/x-wav");
p.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
where "aa.wav" is a wav format song placed in resource folder. when i debug this code
getClass().getResourceAsStream("aa.wav")
it returns null.
Can you please Help me thanks
if resource folder is under src then.
make it
getClass().getResourceAsStream("/resource/aa.wav")